【问题标题】:Php creating image thumb with fixed width and heightPHP创建具有固定宽度和高度的图像拇指
【发布时间】:2016-04-08 12:31:15
【问题描述】:

伙计们,我知道以前有人问过这个问题,但它不一样,因为我尝试了其他答案,但没有任何效果。

我想使用 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);

我不太确定我的 cmets,但我只是写了它们以防万一

我认为它需要一个数学好的人

谢谢,新年快乐

【问题讨论】:

  • 这到底是怎么不适合你的......?
  • 这里有一个提示..您不需要数学来调整图像大小..
  • @MarcB 它没有得到我想要的尺寸
  • @Pamblam 我认为它需要一个方程或比率来将任何宽度和任何高度调整为固定值
  • 不适用于固定的高度和宽度。如果你想缩放图像,你需要数学,但你没有缩放任何东西,你正在调整它的大小并可能改变纵横比..

标签: php image-processing thumbnails


【解决方案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]);

【讨论】:

  • 正是我想要的..我不知道我可以像这样输入宽度和高度
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多