setOptions($options); } public function setOptions(array $options) { if (array_key_exists('options', $options)) { unset($options['options']); } foreach ($options as $name => $value) { $method = 'set' . ucfirst($name); if (method_exists($this, $method)) { $this->$method($value); } else { trigger_error('"' . $name . '" config is not supported'); } } return $this; } public function getImage() { return $this->image; } public function setImage($image) { $this->image = $image; return $this; } public function getImageSize() { list ($width, $height) = getimagesize($this->image); return $width . 'x' . $height; } public function getSizes() { return $this->sizes; } public function setSizes($sizes) { $this->sizes = $sizes; return $this; } public function getItems() { return $this->items; } public function setItems($items) { $this->items = $items; return $this; } public function run() { $this->items = array(); foreach ($this->getSizes() as $size) { $this->items[] = array( 'size' => $size['width'] . 'x' . $size['height'] . ' ' . $size['method'], 'src' => $this->resize($this->image, $size), ); } return $this; } protected function getDestination($image, array $options) { $info = pathinfo($image); return $info['dirname'] . '/' . $info['filename'] . '_' . $options['width'] . 'x' . $options['height'] . '_' . $options['method'] . '.' . $info['extension']; } public function resize($source, $options) { $width = (empty($options['width'])) ? 0 : $options['width']; $height = (empty($options['height'])) ? 0 : $options['height']; $size = $this->getNewSize($source, $width, $height, $options['method']); $cmd = 'convert ' . '-strip -interlace Plane ' . escapeshellarg($source) . ' ' . '-resize ' . $size['width'] . 'x' . $size['height'] . '!'; if ($size['gray']) { $cmd .= ' -colorspace GRAY'; } if (!empty($size['crop'])) { $crop = &$size['crop']; $cmd .= ' -crop ' . $crop['width'] . 'x' . $crop['height'] . '+' . $crop['left'] . '+' . $crop['top'] . ' ' . '+repage'; } $destination = $this->getDestination($source, $options);; $cmd .= ' +profile "*" ' . escapeshellarg($destination); shell_exec($cmd); return $destination; } public function getNewSize($fullPath, $width, $height, $method = 'inner') { if (!is_file($fullPath)) { return array(); } list($oldWidth, $oldHeight, ,) = getimagesize($fullPath); $result = array(); $cropMethod = null; if ($width != 0 || $height != 0) { if ($width == 0) { $width = $oldWidth * $height / $oldHeight; } else if ($height == 0) { $height = $oldHeight * $width / $oldWidth; } $zx = $width / $oldWidth; $zy = $height / $oldHeight; $width = round($width); $height = round($height); $newx = -1; $newy = -1; $result['gray'] = false; if (strstr($method, '_gray')) { $method = str_replace('_gray', '', $method); $result['gray'] = true; } if (0 === strncmp('crop', $method, 4)) { $resizeMethod = 'outer'; $cropMethod = strtolower(substr($method, 4)); } else { $resizeMethod = $method; } switch ($resizeMethod) { case 'outer': if ($zx > $zy) { $z = $zx; $newx = $width; } else { $z = $zy; $newy = $height; } break; case 'inner': default: if ($zx < $zy) { $z = $zx; $newx = $width; } else { $z = $zy; $newy = $height; } break; } $result['width'] = ($newx == -1) ? round($oldWidth * $z) : $newx; $result['height'] = ($newy == -1) ? round($oldHeight * $z) : $newy; } else { $z = 1; $result['width'] = $oldWidth; $result['height'] = $oldHeight; } $result['scale'] = $z; switch ($cropMethod) { case 'first': $result['crop'] = array( 'width' => $width, 'height' => $height, 'left' => 0, 'top' => 0 ); break; case 'center': $result['crop'] = array( 'width' => $width, 'height' => $height, 'left' => round($result['width'] / 2 - $width / 2), 'top' => round($result['height'] / 2 - $height / 2), ); break; case 'last': $result['crop'] = array( 'width' => $width, 'height' => $height, 'left' => $result['width'] > $result['height'] ? $result['width'] - $width : 0, 'top' => $result['width'] > $result['height'] ? 0 : $result['height'] - $height, ); break; default: break; } return $this->_getNormalizedSizeInfo($result); } protected function _getNormalizedSizeInfo($info) { list($width, $height, $scale) = $this->_getNormalizedSize($info['width'], $info['height']); $info['width'] = $width; $info['height'] = $height; $info['scale'] = $info['scale'] * $scale; if (!empty($info['crop'])) { $info['crop']['top'] = round($info['crop']['top'] * $scale); $info['crop']['left'] = round($info['crop']['left'] * $scale); $info['crop']['width'] = round($info['crop']['width'] * $scale); $info['crop']['height'] = round($info['crop']['height'] * $scale); } return $info; } protected function _getNormalizedSize($width, $height) { $scale = 1; if ($width > self::MAX_RESIZE_WIDTH && $height > self::MAX_RESIZE_HEIGHT) { $widthScale = self::MAX_RESIZE_WIDTH / $width; $heightScale = self::MAX_RESIZE_HEIGHT / $height; $scale = ($widthScale < $heightScale) ? $widthScale : $heightScale; } else if ($width > self::MAX_RESIZE_WIDTH) { $scale = self::MAX_RESIZE_WIDTH / $width; } else if ($height > self::MAX_RESIZE_HEIGHT) { $scale = self::MAX_RESIZE_HEIGHT / $height; } if ($scale != 1) { $width = round($width * $scale); $height = round($height * $scale); } return array($width, $height, $scale); } }