您好,登錄后才能下訂單哦!
本篇文章為大家展示了PHP中怎么生成一個(gè)圖片縮略圖類,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
具體如下:
class App_image_helper { protected $imgFileName; protected $imgWidth; protected $imgHeight; protected $imgMime; protected $imgResource; static $imgMineList = array( 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'wbmp' => 'image/wbmp', ); /** * 根據(jù)文件名,初始化圖片, * 計(jì)算出給定圖片的寬、高、圖片類型,并獲取圖片的資源保存到內(nèi)存,便于下次使用 * App_image_helper constructor. * * @param $fileName */ public function __construct($fileName) { $this->imgFileName = $fileName; list($this->imgWidth, $this->imgHeight, $this->imgMime) = $this->getImageInfo($this->imgFileName); $this->imgResource = $this->getImageResource($this->imgFileName); } /** * 根據(jù)圖片路徑獲取相關(guān)寬、高、MIME類型信息 * * @param $fileName * * @return array|null */ protected function getImageInfo($fileName) { $result = null; if ( is_file($fileName) ) { $tmpImageInfo = getimagesize($fileName); if ( $tmpImageInfo ) { $result = array($tmpImageInfo[0], $tmpImageInfo[1], $tmpImageInfo['mime']); } } return $result; } /** * 將圖片文件轉(zhuǎn)為資源類類型 * * @param $fileName * * @return null|resource */ protected function getImageResource($fileName) { $image = null; if ( is_file($fileName) ) { switch ($this->imgMime) { case self::$imgMineList['jpeg']: $image = imagecreatefromjpeg($fileName); break; case self::$imgMineList['gif']: $image = imagecreatefromgif($fileName); break; case self::$imgMineList['png']: $image = imagecreatefrompng($fileName); break; case self::$imgMineList['wbmp']: $image = imagecreatefromwbmp($fileName); break; default: break; } } return $image; } /** * 可根據(jù)固定寬,等比縮放圖片;或根據(jù)百分比,等比縮放圖片 * * @param int $width * @param int $percent * * @return array|null */ protected function getSizeByScale($width = 360, $percent = 1) { $result = null; if ( $this->imgWidth && $this->imgHeight ) { if ( $width ) { $result = array($width, intval($width * $this->imgHeight / $this->imgWidth)); } elseif ( $percent ) { $result = array(intval($this->imgWidth * $percent), intval($this->imgHeight * $percent)); } } return $result; } /** * 外調(diào) * * @param int $percentOrWidth int整數(shù)表示圖片縮放為固定寬度,0.0~0.99999表示縮放百分比 * @param null $fileName * @param int $quality * @param bool $reSample 重新采樣圖片,默認(rèn)是 * * @return bool */ public function createImage($percentOrWidth = 1, $fileName = null, $quality = 75, $reSample = true) { $result = false; $fileName ? header('Content-Type: ' . $this->imgMime) : false; $size = $this->getSizeByScale(($percentOrWidth <= 1) ? null : $percentOrWidth, $percentOrWidth); if ( $size ) { $thumb = imagecreatetruecolor($size[0], $size[1]); if ( $reSample ) { imagecopyresampled($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight); } else { imagecopyresized($thumb, $this->imgResource, 0, 0, 0, 0, $size[0], $size[1], $this->imgWidth, $this->imgHeight); } $result = imagejpeg($thumb, $fileName, $quality); } return $result; } }
上述內(nèi)容就是PHP中怎么生成一個(gè)圖片縮略圖類,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。