/** * Resize image * * @param string $img image path * @param int $w image width * @param int $h image height * @return bool **/ protected function _resizeImg($img, $w, $h) { if (false == ($s = getimagesize($img))) { return false; } switch ($this->_options['imgLib']) { case 'imagick': if (false != ($_img = new imagick($img))) { return $_img->cropThumbnailImage($w, $h) && $_img->writeImage($img); } break; case 'mogrify': exec('mogrify -scale ' . $w . 'x' . $h . '! ' . escapeshellarg($img), $o, $c); return 0 == $c; break; case 'gd': if ($s['mime'] == 'image/jpeg') { $_img = imagecreatefromjpeg($img); } elseif ($s['mime'] = 'image/png') { $_img = imagecreatefrompng($img); } elseif ($s['mime'] = 'image/gif') { $_img = imagecreatefromgif($img); } if (!$_img || false == ($_out = imagecreatetruecolor($w, $h))) { return false; } if (!imagecopyresampled($_out, $_img, 0, 0, 0, 0, $w, $h, $s[0], $s[1])) { return false; } if ($s['mime'] == 'image/jpeg') { $r = imagejpeg($_out, $img, 100); } else { if ($s['mime'] = 'image/png') { $r = imagepng($_out, $img, 7); } else { $r = imagegif($_out, $img, 7); } } imagedestroy($_img); imagedestroy($_out); return $r; break; } }
<?php $case = "show_img"; //show_img, save_img $cropThumbnailImage = true; $filename = "img/" . $_GET["file"]; if (class_exists("imagick")) { $im = new imagick($filename); /* create the thumbnail */ if ($cropThumbnailImage) { $im->cropThumbnailImage(80, 80); } /* Write to a file */ if ($resizeImage) { $im->resizeImage(900, 80, 1, 0.5); } switch ($case) { case "write_img": $im->writeImage("img/" . $_GET["file"]); break; case "show_img": header("Content-Type: image/jpg"); echo $im->getImageBlob(); break; } } else { cropImage_common($filename, 80, 80); } function cropImage_common($filename, $width, $height) { // Content type
function _resizeImagick(&$model, $field, $path, $size, $geometry, $thumbnailPath) { $srcFile = $path . $model->data[$model->alias][$field]; $pathInfo = $this->_pathinfo($srcFile); $thumbnailType = $this->settings[$model->alias][$field]['thumbnailType']; $isMedia = $this->_isMedia($model, $this->runtime[$model->alias][$field]['type']); $image = new imagick(); if ($isMedia) { $image->setResolution(300, 300); $srcFile = $srcFile . '[0]'; } $image->readImage($srcFile); $height = $image->getImageHeight(); $width = $image->getImageWidth(); if (preg_match('/^\\[[\\d]+x[\\d]+\\]$/', $geometry)) { // resize with banding list($destW, $destH) = explode('x', substr($geometry, 1, strlen($geometry) - 2)); $image->thumbnailImage($destW, $destH); } elseif (preg_match('/^[\\d]+x[\\d]+$/', $geometry)) { // cropped resize (best fit) list($destW, $destH) = explode('x', $geometry); $image->cropThumbnailImage($destW, $destH); } elseif (preg_match('/^[\\d]+w$/', $geometry)) { // calculate heigh according to aspect ratio $image->thumbnailImage((int) $geometry - 1, 0); } elseif (preg_match('/^[\\d]+h$/', $geometry)) { // calculate width according to aspect ratio $image->thumbnailImage(0, (int) $geometry - 1); } elseif (preg_match('/^[\\d]+l$/', $geometry)) { // calculate shortest side according to aspect ratio $destW = 0; $destH = 0; $destW = $width > $height ? (int) $geometry - 1 : 0; $destH = $width > $height ? 0 : (int) $geometry - 1; $imagickVersion = phpversion('imagick'); $image->thumbnailImage($destW, $destH, !($imagickVersion[0] == 3)); } if ($isMedia) { $thumbnailType = $this->settings[$model->alias][$field]['mediaThumbnailType']; } if (!$thumbnailType || !is_string($thumbnailType)) { try { $thumbnailType = $image->getImageFormat(); } catch (Exception $e) { $thumbnailType = 'png'; } } $fileName = str_replace(array('{size}', '{filename}'), array($size, $pathInfo['filename']), $this->settings[$model->alias][$field]['thumbnailName']); $destFile = "{$thumbnailPath}{$fileName}.{$thumbnailType}"; $image->setImageCompressionQuality($this->settings[$model->alias][$field]['thumbnailQuality']); $image->setImageFormat($thumbnailType); if (!$image->writeImage($destFile)) { return false; } $image->clear(); $image->destroy(); return true; }
public function _resizeImagick(Model $model, $field, $path, $size, $geometry, $thumbnailPath) { $srcFile = $path . $model->data[$model->alias][$field]; $pathInfo = $this->_pathinfo($srcFile); $thumbnailType = $imageFormat = $this->settings[$model->alias][$field]['thumbnailType']; $isMedia = $this->_isMedia($model, $this->runtime[$model->alias][$field]['type']); $image = new imagick(); if ($isMedia) { $image->setResolution(300, 300); $srcFile = $srcFile . '[0]'; } $image->readImage($srcFile); $height = $image->getImageHeight(); $width = $image->getImageWidth(); if (preg_match('/^\\[[\\d]+x[\\d]+\\]$/', $geometry)) { // resize with banding list($destW, $destH) = explode('x', substr($geometry, 1, strlen($geometry) - 2)); $image->thumbnailImage($destW, $destH, true); $imageGeometry = $image->getImageGeometry(); $x = ($imageGeometry['width'] - $destW) / 2; $y = ($imageGeometry['height'] - $destH) / 2; $image->setGravity(Imagick::GRAVITY_CENTER); $image->extentImage($destW, $destH, $x, $y); } elseif (preg_match('/^[\\d]+x[\\d]+$/', $geometry)) { // cropped resize (best fit) list($destW, $destH) = explode('x', $geometry); $image->cropThumbnailImage($destW, $destH); } elseif (preg_match('/^[\\d]+w$/', $geometry)) { // calculate heigh according to aspect ratio $image->thumbnailImage((int) $geometry - 1, 0); } elseif (preg_match('/^[\\d]+h$/', $geometry)) { // calculate width according to aspect ratio $image->thumbnailImage(0, (int) $geometry - 1); } elseif (preg_match('/^[\\d]+l$/', $geometry)) { // calculate shortest side according to aspect ratio $destW = 0; $destH = 0; $destW = $width > $height ? (int) $geometry - 1 : 0; $destH = $width > $height ? 0 : (int) $geometry - 1; $imagickVersion = phpversion('imagick'); $image->thumbnailImage($destW, $destH, !($imagickVersion[0] == 3)); } if ($isMedia) { $thumbnailType = $imageFormat = $this->settings[$model->alias][$field]['mediaThumbnailType']; } if (!$thumbnailType || !is_string($thumbnailType)) { try { $thumbnailType = $imageFormat = $image->getImageFormat(); // Fix file casing while (true) { $ext = false; $pieces = explode('.', $srcFile); if (count($pieces) > 1) { $ext = end($pieces); } if (!$ext || !strlen($ext)) { break; } $low = array('ext' => strtolower($ext), 'thumbnailType' => strtolower($thumbnailType)); if ($low['ext'] == 'jpg' && $low['thumbnailType'] == 'jpeg') { $thumbnailType = $ext; break; } if ($low['ext'] == $low['thumbnailType']) { $thumbnailType = $ext; } break; } } catch (Exception $e) { $this->log($e->getMessage(), 'upload'); $thumbnailType = $imageFormat = 'png'; } } $fileName = str_replace(array('{size}', '{filename}', '{primaryKey}'), array($size, $pathInfo['filename'], $model->id), $this->settings[$model->alias][$field]['thumbnailName']); $destFile = "{$thumbnailPath}{$fileName}.{$thumbnailType}"; $image->setImageCompressionQuality($this->settings[$model->alias][$field]['thumbnailQuality']); $image->setImageFormat($imageFormat); if (!$image->writeImage($destFile)) { return false; } $image->clear(); $image->destroy(); return true; }
private function greateTH($file, $th) { $im = new imagick($_SERVER['DOCUMENT_ROOT'] . "/" . $file); $im->cropThumbnailImage(110, 110); $im->writeImage($_SERVER['DOCUMENT_ROOT'] . "/" . $th); }
public static function resize($orig_file_path, $settings, $target_file_path) { $quality = 95; $crop = $settings['crop_method']; $current_width = $settings['width']; $current_height = $settings['height']; $target_width = min($current_width, $settings['width_requested']); $target_height = min($current_height, $settings['height_requested']); if ($crop) { $x_ratio = $target_width / $current_width; $y_ratio = $target_height / $current_height; $ratio = min($x_ratio, $y_ratio); $use_x_ratio = $x_ratio == $ratio; $new_width = $use_x_ratio ? $target_width : floor($current_width * $ratio); $new_height = !$use_x_ratio ? $target_height : floor($current_height * $ratio); } else { $new_width = $target_width; $new_height = $target_height; } $im = new imagick($orig_file_path); $im->cropThumbnailImage($new_width, $new_height); $im->setImageCompression(imagick::COMPRESSION_JPEG); $im->setImageCompressionQuality($quality); $im->stripImage(); $result = $im->writeImage($target_file_path); $im->destroy(); return array($new_width, $new_height, $target_width, $target_height); }