php实现根据url自动生成缩略图的方法,url自动生成本文实例讲述了php实现根据url自动生成缩略图的方法,是非常实用的功能。分享给大家供大家参考。具体方法如下:
原理:设置apache rewrite ,当图片不存在时,调用php创建图片。
例如:
原图路径为:http://localhost/upload/news/2013/07/21/1.jpg
缩略图路径为:http://localhost/supload/news/2013/07/21/1.jpg
当访问 http://localhost/supload/news/2013/07/21/1.jpg 时,如图片存在,则显示图片。否则,调用createthumb.php生成图片。
目录结构如下:
www/picthumb.class.php
www/thumbconfig.php
www/upload/news/2013/07/21/1.jpg
www/upload/article/2013/07/21/2.jpg
www/supload/.htaccess
www/supload/watermark.png
www/supload/createthumb.php
http://localhost/ 指向 www目录
picthumb.class.php 用法请查看这里:http://www.bkjia.com/article/55530.htm
需要开启apache rewrite:
sudo a2enmod rewrite
.htaccess文件如下:
rewriteengine on # '-s' (is regular file, with size) # '-l' (is symbolic link) # '-d' (is directory) # 'ornext|or' (or next condition) # 'nocase|nc' (no case) # 'last|l' (last rule) rewritecond %{request_filename} -s [or] rewritecond %{request_filename} -l [or] rewritecond %{request_filename} -d rewriterule ^.*$ - [nc,l] rewriterule ^.*$ createthumb.php?path=%{request_uri} [nc,l]
createthumb.php文件如下:
set_config($config); if($obj->create_thumb($source, $dest)){ ob_clean(); header('content-type:'.mime_content_type($dest)); exit(file_get_contents($dest)); } ?>
thumbconfig.php文件如下:
array( 'fromdir' => 'news', // 来源目录 'type' => 'fit', 'width' => 100, 'height' => 100, 'bgcolor' => '#ff0000' ), 'news_1' => array( 'fromdir' => 'news', 'type' => 'fit', 'width' => 200, 'height' => 200, 'bgcolor' => '#ffff00' ), 'article' => array( 'fromdir' => 'article', 'type' => 'crop', 'width' => 250, 'height' => 250, 'watermark' => www_path.'/supload/watermark.png' ) ); ?>
访问这三个路径后会按config自动生成缩略图
http://localhost/supload/news/2013/07/21/1.jpg
http://localhost/supload/news_1/2013/07/21/1.jpg
http://localhost/supload/article/2013/07/21/2.jpg
本文所述实例完整代码点击此处本站下载。
希望本文所述对大家的php程序设计有所帮助。
url实现缩略图
需要php环境支持gd库。
以上代码可以根据一个图片生成400*300的缩略图,如:
www.xx.com/image.php?img=1.jpg
要求1.jpg必须存在,大小任意。并且1.jpg和image.php在同一目录下。
www.xx.com/image.php?img=upload2009/1.jpg
也可以,不用改了,就用上面的。反正$img_name变量就是图片的url。你自己根据实际情况看着改吧。
php自动生成缩略图代码
给你个函数吧
// *****生成缩略图*****
// 只考虑jpg,png,gif格式
// $srcimgpath 源图象路径
// $targetimgpath 目标图象路径
// $targetw 目标图象宽度
// $targeth 目标图象高度
function makethumbnail($srcimgpath,$targetimgpath,$targetw,$targeth)
{
$imgsize = getimagesize($srcimgpath);
$imgtype = $imgsize[2];
//@ 使函数不向页面输出错误信息
switch ($imgtype)
{
case 1:
$srcimg = @imagecreatefromgif($srcimgpath);
break;
case 2:
$srcimg = @imagecreatefromjpeg($srcimgpath);
break;
case 3:
$srcimg = @imagecreatefrompng($srcimgpath);
break;
}
//取源图象的宽高
$srcw = imagesx($srcimg);
$srch = imagesy($srcimg);
if($srcw>$targetw || $srch>$targeth)
{
$targetx = 0;
$targety = 0;
if ($srcw > $srch)
{
$finaw=$targetw;
$finalh=round($srch*$finaw/$srcw);
$targety=floor(($targeth-$finalh)/2);
}
else
{
$finalh=$targeth;
$finaw=round($srcw*$finalh/$srch);
$targetx=floor(($targetw-$finaw)/2);
}
//function_exists 检查函数是否已定义
//imagecreatetruecolor 本函数需要gd2.0.1或更高版本
if(function......余下全文>>
http://www.bkjia.com/phpjc/882893.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/882893.htmltecharticlephp实现根据url自动生成缩略图的方法,url自动生成 本文实例讲述了php实现根据url自动生成缩略图的方法,是非常实用的功能。分享给大家供...
