public function resize($src_file, $target_file, $width, $height, $type, $new_width, $new_height) { $obj = new ecae_image(); $obj->set_file($src_file); $obj->resize($new_width, $new_height); $obj->strip(); $content = $obj->exec(); if ($content) { file_put_contents($target_file, $content); return true; } else { return false; } }
/** * 生成指定宽度和高度的图片 * @param object image model object * @param string source file directory * @param mixed 临时数据源 * @param string 宽度 * @param string 高度 * @return null */ function image_resize(&$imgmdl, $src_file, $target_file, $new_width, $new_height) { if (isset($src_file) && is_file($src_file)) { list($width, $height, $type) = getimagesize($src_file); $size = self::get_image_size($new_width, $new_height, $width, $height); $new_width = $size[0]; $new_height = $size[1]; if (ECAE_MODE) { include_lib('image.php'); $obj = new ecae_image(); $obj->set_file($src_file); $obj->resize($new_width, $new_height); $obj->strip(); $content = $obj->exec(); if ($content) { file_put_contents($target_file, $content); return true; } else { return false; } } elseif (function_exists('magickresizeimage')) { $rs = NewMagickWand(); if (MagickReadImage($rs, $src_file)) { MagickResizeImage($rs, $new_width, $new_height, MW_QuadraticFilter, 0.3); MagickSetImageFormat($rs, 'image/jpeg'); MagickWriteImage($rs, $target_file); } return true; } elseif (function_exists('imagecopyresampled')) { $quality = 80; $image_p = imagecreatetruecolor($new_width, $new_height); if ($new_width > $width && $new_height > $height) { $background_color = imagecolorallocate($image_p, 255, 255, 255); imagefilledrectangle($image_p, 0, 0, $new_width, $new_height, $background_color); } imagealphablending($image_p, false); switch ($type) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($src_file); $func = 'imagejpeg'; break; case IMAGETYPE_GIF: $image = imagecreatefromgif($src_file); $func = 'imagegif'; break; case IMAGETYPE_PNG: $image = imagecreatefrompng($src_file); imagesavealpha($image, true); $func = 'imagepng'; $quality = 8; break; } imagesavealpha($image_p, true); if ($new_width > $width && $new_height > $height) { imagecopyresampled($image_p, $image, ($new_width - $width) / 2, ($new_height - $height) / 2, 0, 0, $width, $height, $width, $height); } else { imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } if ($func) { $func($image_p, $target_file, $quality); } imagedestroy($image_p); imagedestroy($image); } } }