Php创建具有固定宽度和高度的图像拇指

时间:2016-01-04 20:00:06

标签: php image-processing thumbnails

伙计们,我知道这个问题之前会被问到,但不一样,因为我已经尝试了其他答案,没有任何工作可以按照我的意愿。

我想使用php和缩略图 width =“265px” height =“125px”创建缩略图,这是我的代码:

$image_width = $imageSize[0];
$image_height = $imageSize[1];

$new_size = ($image_width + $image_height) / ($image_width * ($image_height / 45));         //this will set any image to 125*70 which is a good thumbnail

$new_width = $image_width * $new_size;
$new_height = $image_height * $new_size;

if($ext == "gif"){ 
    $old_image = imagecreatefromgif($real_image_path);
}else if($ext =="png"){ 
    $old_image = imagecreatefrompng($real_image_path);
}else{ 
    $old_image = imagecreatefromjpeg($real_image_path);
}

$new_image = imagecreatetruecolor($new_width, $new_height);                                                     //creating new image with a new color for quality

imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);

我对我的评论不太确定,但我只是为了以防万一而写了

我认为需要一个擅长数学的人

感谢新年快乐

1 个答案:

答案 0 :(得分:1)

无需数学......

if($ext == "gif"){ 
    $old_image = imagecreatefromgif($real_image_path);
}else if($ext =="png"){ 
    $old_image = imagecreatefrompng($real_image_path);
}else{ 
    $old_image = imagecreatefromjpeg($real_image_path);
}
$data = getimagesize($real_image_path);

$height = 125;
$width = 265;
$thumb = imagecreatetruecolor($width, $height);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled($thumb, $old_image, 0, 0, 0, 0, $width, $height, $data[0], $data[1]);