使用示例:
http://resource.ecsq.com/getpic.php?src=/image/goods/SUZHOU/201405/102389_1400495466458.jpg&w=500&h=500
(一)getpic.php
<?php //获取图片参数 $basedir = dirname(dirname(__FILE__))."/file/"; $src = isset($_GET['src'])?$_GET['src']:NULL; $w = isset($_GET['w'])?$_GET['w']:200; $h = isset($_GET['h'])?$_GET['h']:''; $src=\'#\'" //原图是否存在 if(!$src) { //$src = 'defaultImage/no_photo01.gif'; $src = 'images/nullpic/nullpic1.jpg'; } //缓存图片 $tempfile = dirname(__FILE__).'/temp/'.$w.'x'.$h.'_'.str_replace(array('/','\\'), '_', $src); if(file_exists($tempfile) && filemtime($tempfile)>filemtime($src)) { $src = $tempfile; } else { require_once 'gcImage.php'; list($swidth,$sheight) = getimagesize($src); $image = new gcImage(); if(empty($h)) $h = $w * $sheight/$swidth; $src = $image->imagickmakethumb($src, $tempfile, $w, $h,90); } if(empty($src)) { //$src = 'defaultImage/no_photo01.gif'; $src = 'images/nullpic/nullpic1.jpg'; } //下载该图片 $file_extension = strtolower(substr(strrchr($src,'.'),1)); switch($file_extension) { case 'gif': $file_mime='image/gif'; break; case 'png': $file_mime='image/png'; break; case 'jpg': $file_mime='image/jpg'; break; default: $file_mime=mime_content_type($src); } $length = filesize($src); Header("Content-type: $file_mime; charset=UTF-8"); Header('Accept-Ranges: bytes'); Header('Accept-Length: '.$length); //Header('Content-Disposition: attachment; filename=' . $file_name); $file = fopen($src,'r'); echo fread($file,$length); fclose($file);
(2)gcImage.php
<?php /** * 画像操作 * gcImageMagickService * @author Founder * @version 1.0 * @exlcude 作成日:2010/11/05 */ class gcImage { /*构造函数-生成缩略图+水印,参数说明: $srcFile-图片文件名, $dstFile-另存文件名, $markwords-水印文字, $markimage-水印图片, $dstW-图片保存宽度, $dstH-图片保存高度, $rate-图片保存品质*/ //makethumb("a.jpg","b.jpg","50","50"); function makethumb($srcFile, $dstFile, $dstW, $dstH, $rate = 100, $markwords = null, $markimage = null) { $data = GetImageSize ( $srcFile ); switch ($data [2]) { case 1 : $im = @ImageCreateFromGIF ( $srcFile ); break; case 2 : $im = @ImageCreateFromJPEG ( $srcFile ); break; case 3 : $im = @ImageCreateFromPNG ( $srcFile ); break; } if (! $im) return False; $srcW = ImageSX ( $im ); $srcH = ImageSY ( $im ); $dstX = 0; $dstY = 0; if ($srcW * $dstH < $srcH * $dstW) { $fdstH = round ( $srcH * $dstW / $srcW ); $dstY = floor ( ($dstH - $fdstH) / 2 ); $fdstW = $dstW; } else { $fdstW = round ( $srcW * $dstH / $srcH ); $dstX = floor ( ($dstW - $fdstW) / 2 ); $fdstH = $dstH; } $ni = ImageCreateTrueColor ( $dstW, $dstH ); $dstX = ($dstX > ($dstW / 2)) ? floor ( $dstW / 2 ) : $dstX; $dstY = ($dstY > ($dstH / 2)) ? floor ( $dstH / 2 ) : $dstY; $white = ImageColorAllocate ( $ni, 255, 255, 255 ); $black = ImageColorAllocate ( $ni, 0, 0, 0 ); imagefilledrectangle ( $ni, 0, 0, $dstW, $dstH, $white ); // 填充背景色 ImageCopyResized ( $ni, $im, $dstX, $dstY, 0, 0, $fdstW, $fdstH, $srcW, $srcH ); if ($markwords != null) { $markwords = iconv ( "gb2312", "UTF-8", $markwords ); //转换文字编码 ImageTTFText ( $ni, 20, 30, 450, 560, $black, "simhei.ttf", $markwords ); //写入文字水印 //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容 } elseif ($markimage != null) { $wimage_data = GetImageSize ( $markimage ); switch ($wimage_data [2]) { case 1 : $wimage = @ImageCreateFromGIF ( $markimage ); break; case 2 : $wimage = @ImageCreateFromJPEG ( $markimage ); break; case 3 : $wimage = @ImageCreateFromPNG ( $markimage ); break; } imagecopy ( $ni, $wimage, 500, 560, 0, 0, 88, 31 ); //写入图片水印,水印图片大小默认为88*31 imagedestroy ( $wimage ); } ImageJpeg ( $ni, $dstFile, $rate ); touch($dstFile); imagedestroy ( $ni ); imagedestroy ( $im ); return $dstFile; } /*构造函数-生成缩略图+水印,参数说明: $srcFile-图片文件名, $dstFile-另存文件名, $markwords-水印文字, $markimage-水印图片, $dstW-图片保存宽度, $dstH-图片保存高度, $rate-图片保存品质*/ //makethumb("a.jpg","b.jpg","50","50"); function imagickmakethumb($srcFile, $dstFile, $dstW, $dstH, $rate = 100, $markwords = null, $markimage = null) { try { $image=new Imagick($srcFile); $image->setImageCompression(Imagick::COMPRESSION_JPEG); $image->setImageCompressionQuality($rate); $image->setImageFormat('JPEG'); $image->cropThumbnailImage ($dstW,$dstH); $image->stripImage(); if (!empty($markwords)) { $draw = new ImagickDraw(); $draw->setGravity(Imagick::GRAVITY_SOUTHEAST); $draw->setFont('simhei.ttf'); $draw->setFontSize(12); $draw->setFillColor('#000'); $image->annotateImage($draw, 6, 6, 0, $markwords); } elseif(!empty($markimage)) { } $image->writeImage($dstFile); $image->clear(); $image->destroy(); return $dstFile; } catch (Exception $e) { return false; } } }