缩略图,水印
生成缩列图有二种方式:
一,提前生成好,供调用
缩列图常规做法是,开始根据网站中的图片规格,要生成多少种缩列图,图片上传过程中就会生成缩列,供不同需求去调用。像这种方法有一点不好就是,如果增加图片规格以后,以前上传的图片没有该规格的,还要重新生成。见附件
二,调用的时候在生成缩列图。这也是我写这篇博客原因。
推荐一个timthumb,个人觉得在不考虑性能,效率的情况下,这个东西,绝对是好用的。
1,下载地址:http://timthumb.googlecode.com/svn/trunk/timthumb.php
2,使用方法
下载下来后,放到你的web环境中,在与timthumb.php平行的目录中建一个cache文件夹,给于755权限,windows的话,就不用了。测试代码如下:
<?php $image = "http://localhost/test/pen_pic03.png"; //$ALLOWED_SITES 需设置 ?> <html> <head> <title>timthumb</title> </head> <body> <img src="http://localhost/test/timthumb.php?src=<?php echo $image; ?>&w=58&h=46&zc=1" alt="" /> </body> </html>
参数说明:
w=58 表示图片宽为58
h=46 表示图片高为46
zc = 1/2/3
可以根据需要,随便设置。
timthumb最大的优点就是方便,目前还没有遇到过这样的工具,缺点同样明显,每次请求的时候,都会调用php-cgi或者是php-cli,缓存的数据放在cache目录下,调用数据要去查找目录,性能上会比较低。
<?php /** * 不变型裁剪及图片按比例裁剪 */ class resizeimage { /** *$source_path传入的图片路径 *$target_width生成缩略图的宽度 *$target_height生成缩略图的高度 *$save_path生成之后保存的路径 *$output 浏览器输出 */ public function imagecropper($source_path, $target_width, $target_height, $save_path = '', $output = true) { $filename = basename($source_path); //获取传入的文件名 $fname = explode('.', $filename); $newfile = $save_path . $fname[0] . "_$target_width" . "_$target_height." . $fname[1]; if (file_exists($newfile)) //判断文件是否存在,存在就不生成 { if ($output) { header('Content-Type: image/jpeg'); readfile($newfile); //输出图像 } die; } $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_ratio = $source_height / $source_width; $target_ratio = $target_height / $target_width; // 源图过高 if ($source_ratio > $target_ratio) { $cropped_width = $source_width; $cropped_height = $source_width * $target_ratio; $source_x = 0; $source_y = ($source_height - $cropped_height) / 2; } // 源图过宽 elseif ($source_ratio < $target_ratio) { $cropped_width = $source_height / $target_ratio; $cropped_height = $source_height; $source_x = ($source_width - $cropped_width) / 2; $source_y = 0; } // 源图适中 else { $cropped_width = $source_width; $cropped_height = $source_height; $source_x = 0; $source_y = 0; } switch ($source_mime) { case 'image/gif': $source_image = imagecreatefromgif($source_path); break; case 'image/jpeg': $source_image = imagecreatefromjpeg($source_path); break; case 'image/png': $source_image = imagecreatefrompng($source_path); break; default: return false; break; } $target_image = imagecreatetruecolor($target_width, $target_height); $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); // 裁剪 imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); // 缩放 imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); if ($output) { header('Content-Type: image/jpeg'); imagejpeg($target_image); //输出图像 } imagejpeg($target_image, $newfile, 80); imagedestroy($source_image); imagedestroy($target_image); imagedestroy($cropped_image); } } ?>
相关推荐
SwfUpload在MVC4下多文件上传缩略图水印例子
在网上找了好久都没有一个好的图片上传组建,于是我自己弄了一个, 功能非常 强大。...能够显示缩略图,一次上传多张,生成缩略图,按比例缩放。添加水印都有。而且能够兼容时下的各种浏览器。 Jquery
上传图片到服务器端,有缩略图,有水印,需要的可以看看,挺不错的~
4.原图自定义删除; 5.图像亮度、对比度、RGB颜色值的调整; 6.可获取生成后的图片路径; 7.图片无损生成; 8.水印文字阴影禁启用; 9.水印文字中文字竖形排列,增加文字纵横排列参数开关; 10.调用将灵活; 11.优化水印...
php图片文件上传 水印缩略图php图片文件上传 水印缩略图
Asp.net上传图片同时生成缩略图和水印图
PHP图片处理(上传、加水印、缩略图),对图片的处理
说明:生成上传图片的缩略图和为缩略图添加水印;
1、多图片选择、上传、水印、缩略功能 2、略缩图片,大中小三种 3、添加图片检测,限制图片大小 4、略缩图自动分类 5、自主设置是否添加文字水印 6、自主设置是否添加图片水印 7、自主设置水印位置 注释:...
用fileUpload上传图片同时生成水印图片和缩略图
asp.net图片上传自动生成缩略图、图片水印、文字水印
图片上传,以及图片加水印,上传后生成相应的缩略图。
PHP上传多图片,带有生成缩略图,自动加水印功能的类
PHP上传类+水印(文字/图片)+缩略图 该类包含了上传文件自定义类型、图片文件开启缩略图以及开启文字水印和图片水。 直接调用canupload方法即可
图片上传:生成缩略图 加文字水印 图片水印 51aspx.png为水印图片 远程图片抓取(保存到本地)支持jpg、gif、bmp、png 图片抓取后自动以Auto+日期+原名称命名 输入远程图片地址(支持Html)
自己写的控件,支持批量文件和图片上传,可以为图片添加多种尺寸的缩略图,支持图片和文字水印,调用简单,多种参数设置
加水印、生成缩略图、远程...图片上传:生成缩略图 加文字水印 图片水印 51aspx.png为水印图片 远程图片抓取(保存到本地)支持jpg、gif、bmp、png 图片抓取后自动以Auto+日期+原名称命名 输入远程图片地址(支持Html)
struts2上传文件、生成缩略图、添加文字和图片水印,