我想从图像创建缩略图,但是固定尺寸 - 宽310px,高217px。
我的代码:
list($width, $height) = getimagesize($filename);
$width = 310;
$height = 217;
$new_width = 310;
$new_height = 217;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);......
我该怎么做?公式是什么?
答案 0 :(得分:3)
function resizeImage($url, $width, $height, $url_out, $keep_ratio = false){
if($height <= 0 && $width <= 0)
return false;
else{
copy($url, $url_out);
$info = getimagesize($url);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
if($keep_ratio){
if($width == 0)
$factor = $height/$height_old;
elseif($height == 0)
$factor = $width/$width_old;
else
$factor = min($width / $width_old, $height / $height_old);
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
}
else{
$final_width = ($width <= 0) ? $width_old : $width;
$final_height = ($height <= 0) ? $height_old : $height;
}
switch($info[2]){
case IMAGETYPE_GIF:
$image = imagecreatefromgif($url_out);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($url_out);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($url_out);
break;
default:
return false;
}
$image_resized = imagecreatetruecolor($final_width, $final_height);
if($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_PNG){
$transparency = imagecolortransparent($image);
if($transparency >= 0){
$transparent_color = imagecolorsforindex($image, $trnprt_indx);
$transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif($info[2] == IMAGETYPE_PNG){
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
switch($info[2]){
case IMAGETYPE_GIF:
imagegif($image_resized, $url_out);
break;
case IMAGETYPE_JPEG:
imagejpeg($image_resized, $url_out);
break;
case IMAGETYPE_PNG:
imagepng($image_resized, $url_out);
break;
default:
return false;
}
imagedestroy($image_resized);
return true;
}
}
//只需调用该函数,然后更改图像名称
$url="desert09.jpg";
resizeImage($url, 310, 217,"output.jpg", $keep_ratio = false);
答案 1 :(得分:2)
我有这个代码(我不记得我在哪里得到它),我已经编辑了一下。我希望它可以帮到你,这会创建一个固定的方形大小拇指:
/** Create a square cropped thumb **/
function createSquareCroppedThumb($path , $thumbPath, $thumbSize = 100 ){
global $max_width, $max_height;
/* Set Filenames */
$srcFile = $path;
$thumbFile = $thumbPath;
/* Determine the File Type */
$type = pathinfo($path, PATHINFO_EXTENSION);
/* Create the Source Image */
switch( $type ){
case 'jpg' : case 'jpeg' :
$src = imagecreatefromjpeg( $srcFile ); break;
case 'png' :
$src = imagecreatefrompng( $srcFile ); break;
case 'gif' :
$src = imagecreatefromgif( $srcFile ); break;
}
/* Determine the Image Dimensions */
$oldW = imagesx( $src );
$oldH = imagesy( $src );
$minValue = $oldH > $oldW ? $oldW : $oldH;
/* Create the New Image */
$new = imagecreatetruecolor( $thumbSize , $thumbSize );
/* Transcribe the Source Image into the New (Square) Image */
imagecopyresampled($new , $src , 0 , 0 , ($oldW / 2) - ($minValue /2) , ($oldH / 2) - ($minValue /2) , $thumbSize , $thumbSize , $minValue , $minValue );
switch( $type ){
case 'jpg' : case 'jpeg' :
$src = imagejpeg( $new , $thumbFile ); break;
case 'png' :
$src = imagepng( $new , $thumbFile ); break;
case 'gif' :
$src = imagegif( $new , $thumbFile ); break;
}
imagedestroy( $new );
}