您好,登錄后才能下訂單哦!
php如何實現(xiàn)截取不同規(guī)格尺寸的圖片?,相信大部分人都還沒學會這個技能,為了讓大家學會,給大家總結(jié)了以下內(nèi)容,話不多說,一起往下看吧。
(一)getpic.php
<?php //獲取圖片參數(shù) $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 { /*構(gòu)造函數(shù)-生成縮略圖+水印,參數(shù)說明: $srcFile-圖片文件名, $dstFile-另存文件名, $markwords-水印文字, $markp_w_picpath-水印圖片, $dstW-圖片保存寬度, $dstH-圖片保存高度, $rate-圖片保存品質(zhì)*/ //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 ); //轉(zhuǎn)換文字編碼 ImageTTFText ( $ni, 20, 30, 450, 560, $black, "simhei.ttf", $markwords ); //寫入文字水印 //參數(shù)依次為,文字大小|偏轉(zhuǎn)度|橫坐標|縱坐標|文字顏色|文字類型|文字內(nèi)容 } 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; } /*構(gòu)造函數(shù)-生成縮略圖+水印,參數(shù)說明: $srcFile-圖片文件名, $dstFile-另存文件名, $markwords-水印文字, $markp_w_picpath-水印圖片, $dstW-圖片保存寬度, $dstH-圖片保存高度, $rate-圖片保存品質(zhì)*/ //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實現(xiàn)截取不同規(guī)格尺寸的圖片的方法,詳細使用情況還需要大家自己親自動手使用過才能領(lǐng)會。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。