拇指生成飞行

时间:2012-01-23 09:04:31

标签: php

我使用以下代码将大图像转换为较小的图像...

$filename= $_GET['filename'];
$width = $_GET['width'];
$height = $_GET['height'];
$path="http://localhost/pics/"; //finish in "/"


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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($path.$filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($path.$filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

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

// Imagedestroy
imagedestroy ($image_p);

它的工作完美,我如何缓存图像?请帮助我。感谢。

3 个答案:

答案 0 :(得分:2)

为什么不保存缩略图,然后你可以调用这样的缩略图:

'<img src="/path/to/cache/thumbnail.jg" />'

我为此修改了代码:

<?php
$filename= $_GET['filename'];
$width = $_GET['width'];
$height = $_GET['height'];
$path="http://localhost/pics/"; //finish in "/"



// Get new dimensions
list($width_orig, $height_orig) = getimagesize($path.$filename);

if ($width && ($width_orig < $height_orig)) {
   $width = ($height / $height_orig) * $width_orig;
} else {
   $height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($path.$filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p,'/path/to/cache/'.$filename, 100);

// Imagedestroy
imagedestroy ($image_p);

?>

答案 1 :(得分:1)

imagejpeg的第二个参数是保存图像的$ filename。只需将图像保存到磁盘即可。

答案 2 :(得分:0)

在我的观点中,缓存图像的最佳方法是使用eTag

有关Yahoo!

的更多信息

设置最小CND并不容易,但会提升您的网站效果!

我非常确定有很多可以帮助您入门的最小脚本。