保存图像使用PHP调整大小

时间:2010-10-16 19:33:54

标签: php image resize save editing

我正在努力改进我的Facebook应用程序。我需要能够调整图像大小,然后将其保存到服务器上的目录中。这是我必须调整大小的代码:

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

我的问题是,如何保存此已调整大小的图像?我需要吗?有没有办法操纵已调整大小的图像而不保存

3 个答案:

答案 0 :(得分:8)

根据manual on imagejpeg(),可选的第二个参数可以指定一个文件名,它将被写入。

  

<强>文件名

     

将文件保存到的路径。如果未设置或为NULL,则原始图像流将直接输出。

     

要跳过此参数以提供质量参数,请使用NULL。

将结果写入磁盘进行一些基本缓存通常是个好主意,这样每次传入的请求都不会导致(资源密集型)GD调用。

答案 1 :(得分:3)

function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}

它应该是工作

答案 2 :(得分:1)

相关问题