您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
php如何实现截取不同规格尺寸的图片?,相信大部分人都还没学会这个技能,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。
(一)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; } } }
以上就是php实现截取不同规格尺寸的图片的方法,详细使用情况还需要大家自己亲自动手使用过才能领会。如果想了解更多相关内容,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。