使用示例:
http://resource.ecsq.com/getpic.php?src=/p_w_picpath/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= $basedir.$src; //原图是否存在 if(!$src) { //$src = 'defaultImage/no_photo01.gif'; $src = 'p_w_picpaths/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) = getp_w_picpathsize($src); $p_w_picpath = new gcImage(); if(empty($h)) $h = $w * $sheight/$swidth; $src = $p_w_picpath->imagickmakethumb($src, $tempfile, $w, $h,90); } if(empty($src)) { //$src = 'defaultImage/no_photo01.gif'; $src = 'p_w_picpaths/nullpic/nullpic1.jpg'; } //下载该图片 $file_extension = strtolower(substr(strrchr($src,'.'),1)); switch($file_extension) { case 'gif': $file_mime='p_w_picpath/gif'; break; case 'png': $file_mime='p_w_picpath/png'; break; case 'jpg': $file_mime='p_w_picpath/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: p_w_upload; 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-水印文字, $markp_w_picpath-水印图片, $dstW-图片保存宽度, $dstH-图片保存高度, $rate-图片保存品质*/ //makethumb("a.jpg","b.jpg","50","50"); function makethumb($srcFile, $dstFile, $dstW, $dstH, $rate = 100, $markwords = null, $markp_w_picpath = 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 ); p_w_picpathfilledrectangle ( $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 ($markp_w_picpath != null) { $wp_w_picpath_data = GetImageSize ( $markp_w_picpath ); switch ($wp_w_picpath_data [2]) { case 1 : $wp_w_picpath = @ImageCreateFromGIF ( $markp_w_picpath ); break; case 2 : $wp_w_picpath = @ImageCreateFromJPEG ( $markp_w_picpath ); break; case 3 : $wp_w_picpath = @ImageCreateFromPNG ( $markp_w_picpath ); break; } p_w_picpathcopy ( $ni, $wp_w_picpath, 500, 560, 0, 0, 88, 31 ); //写入图片水印,水印图片大小默认为88*31 p_w_picpathdestroy ( $wp_w_picpath ); } ImageJpeg ( $ni, $dstFile, $rate ); touch($dstFile); p_w_picpathdestroy ( $ni ); p_w_picpathdestroy ( $im ); return $dstFile; } /*构造函数-生成缩略图+水印,参数说明: $srcFile-图片文件名, $dstFile-另存文件名, $markwords-水印文字, $markp_w_picpath-水印图片, $dstW-图片保存宽度, $dstH-图片保存高度, $rate-图片保存品质*/ //makethumb("a.jpg","b.jpg","50","50"); function imagickmakethumb($srcFile, $dstFile, $dstW, $dstH, $rate = 100, $markwords = null, $markp_w_picpath = null) { try { $p_w_picpath=new Imagick($srcFile); $p_w_picpath->setImageCompression(Imagick::COMPRESSION_JPEG); $p_w_picpath->setImageCompressionQuality($rate); $p_w_picpath->setImageFormat('JPEG'); $p_w_picpath->cropThumbnailImage ($dstW,$dstH); $p_w_picpath->stripImage(); if (!empty($markwords)) { $draw = new ImagickDraw(); $draw->setGravity(Imagick::GRAVITY_SOUTHEAST); $draw->setFont('simhei.ttf'); $draw->setFontSize(12); $draw->setFillColor('#000'); $p_w_picpath->annotateImage($draw, 6, 6, 0, $markwords); } elseif(!empty($markp_w_picpath)) { } $p_w_picpath->writeImage($dstFile); $p_w_picpath->clear(); $p_w_picpath->destroy(); return $dstFile; } catch (Exception $e) { return false; } } }