日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php图片生成缩略图_PHP生成图片缩略图类示例

發布時間:2025/3/20 php 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php图片生成缩略图_PHP生成图片缩略图类示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了PHP生成圖片縮略圖類。分享給大家供大家參考,具體如下:

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',

);

/**

* 根據文件名,初始化圖片,

* 計算出給定圖片的寬、高、圖片類型,并獲取圖片的資源保存到內存,便于下次使用

* 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);

}

/**

* 根據圖片路徑獲取相關寬、高、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;

}

/**

* 將圖片文件轉為資源類類型

*

* @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;

}

/**

* 可根據固定寬,等比縮放圖片;或根據百分比,等比縮放圖片

*

* @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;

}

/**

* 外調

*

* @param int $percentOrWidth int整數表示圖片縮放為固定寬度,0.0~0.99999表示縮放百分比

* @param null $fileName

* @param int $quality

* @param bool $reSample 重新采樣圖片,默認是

*

* @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;

}

}

PS:這里再為大家推薦幾款比較實用的圖片處理工具供大家參考使用:

希望本文所述對大家PHP程序設計有所幫助。

總結

以上是生活随笔為你收集整理的php图片生成缩略图_PHP生成图片缩略图类示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。