Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拇指动态生成_Php - Fatal编程技术网

Php 拇指动态生成

Php 拇指动态生成,php,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, $heig

我正在使用以下代码将大图像转换为小图像

$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);
$filename=$\u GET['filename'];
$width=$_GET['width'];
$height=$_获取['height'];
$path=”http://localhost/pics/"; //以“/”结束
//内容类型
标题(“内容类型:图像/jpeg”);
//获取新维度
列表($width\u orig,$height\u orig)=getimagesize($path.$filename);
如果($width&&($width\u orig<$height\u orig)){
$width=($height/$height\u orig)*$width\u orig;
}否则{
$height=($width/$width\u orig)*$height\u orig;
}
//重采样
$image\u p=imageCreateTureColor($width,$height);
$image=imagecreatefromjpeg($path.$filename);
imagecopyresampled($image\u p,$image,0,0,0,$width,$height,$width\u orig,$height\u orig);
//输出
imagejpeg($image_p,null,100);
//图像销毁
图像销毁($image\u p);

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

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

在我的建议中,缓存图像的最佳方法是使用

更多关于

这是不容易设置一个最低限度的,但它提高了你的网站


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

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

我修改了您的代码:

<?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);

?>


除非你真的想自己学习-为什么不使用现有的缩略图库进行开箱即用的缓存?你能给我推荐一些吗?谷歌
php缩略图生成器
并使用第一个hiti使用smarty btw,这对缓存有帮助吗?不,你需要一个单独的缩略图生成器。我正在使用smarty,这对缓存有帮助吗?缓存的目的是防止脚本必须为每个请求运行。将映像存储到磁盘将实现这一点。在运行缩略图代码之前,您只需检查(文件_是否存在('/path/to/cache/'.$filename))。我已经很长时间没有使用Smarty了,所以它可能有一些方法来处理这个问题,但在我看来,你最好保持简单。如果你删除/更新大图像,那么你必须删除/更新缩略图。