PHP图像重新调整大小

时间:2013-01-10 14:14:36

标签: php

我有一个PHP脚本可以重新调整JPEG图像的大小。但是,由于某种原因图像被扭曲,即使我编程它按比例计算x或y(取决于照片方向)。质量是100,所以我不明白为什么它会使它们扭曲。我做错了什么?

修改

原始图像为3264px x 2448px

原始图片: http://imgur.com/DOsKf&hMAOh#0

重新调整大小: http://imgur.com/DOsKf&hMAOh#1

由于

守则:

<?php

$im = ImageCreateFromJpeg('IMG_0168.jpg');

//Find the original height and width.

$ox = imagesx($im);
$oy = imagesy($im);

//Now we will determine the new height and width. For this example maximum height will    
be 500px and the width will be 960px. To prevent inproper proportions we need to know 
if the image is portrate or landscape then set one dimension and caluate the other. 

$height = 500;
$width = 960;
if($ox < $oy)   #portrate
{
   $ny = $height;
   $nx = floor($ox * ($ny / $oy)); 
} 
else #landscape
{
   $nx = $width;
   $ny = floor($oy * ($nx / $ox)); 
} 

//Then next two functions will create a new image resource then copy the original image     
to the new one and resize it.

$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $ny, $ox, $oy);

//Now we just need to save the new file.

imagejpeg($nm, 'smallerimagefile2.jpg', 100);

?>

2 个答案:

答案 0 :(得分:5)

使用

  

imagecopyresampled($ nm,$ im,0,0,0,0,$ nx,$ ny,$ ox,$ oy);

而不是

  

imagecopyresized($ nm,$ im,0,0,0,0,$ nx,$ ny,$ ox,$ oy);

说明:

imagecopyresized()允许您快速轻松地更改图像的大小,但是产生相当低质量图片的缺点。 imagecopyresampled()采用与imagecopyresized()相同的参数,并以相同的方式工作,但调整了调整大小的图像的例外。缺点是平滑需要更多的CPU工作量,因此生成图像需要更长的时间。

功能详情:

答案 1 :(得分:0)

完全取决于缩放过程中使用的算法。如果您使用像Gimp这样的图像编辑器,您将看到它可以使用的不同插值算法(即线性,立方等)。算法越复杂,缩放的图像结果越好,但处理的处理越多,所以需要的时间越长。

我不知道你是否可以改变GD使用的算法,但我相信你可以在PHP中使用ImageMagick库。