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;
}
}
-
PHP软件工程师的笔试题
导语:作为计算机专业毕业生的你,知道多少PHP软件工程师的笔试题。下面的是本站小编为大家搜集的PHP软件工程师的笔试题,供大家参考。1.基本知识点HTTP协议中几个状态码的含义:503500401200301302。。。Includerequireinclude_oncerequire_once的区别.PHP/Mysql中...
-
用PHP遍历目录下的全部文件
项目中有这么一个需求,需要遍历某个文件夹(目录)下的'所有文件。下面是解决这个问题所写的PHP代码,欢迎阅读,更多消息请关注应届毕业生网。第一种实现办法是用dir返回对象,第二种实现办法:用readdir()函数。/**********************一个简单的目录递归函数第一种实...
-
PHP读取文件的正确方法
处理诸如PHP之类的现代编程语言的乐趣之一就是有大量的选项可用。PHP读取文件的正确方法是怎么样的呢?下面是小编为大家整理的PHP读取文件的正确方法,欢迎参考~传统的fopen方法fopen方法可能是以前的C和C++程序员最熟悉的,因为如果您使用过这些语言,那么它们或多或...
-
php如何基于dom实现图书xml格式数据
导语:php如何基于dom实现图书xml格式数据呢?下面是小编给大家提供的代码实现方法,大家可以参考阅读,更多详情请关注应届毕业生考试网。<?php$doc=newDOMDocument();$doc->load('');$books=$doc->getElementsByTagName("book");foreach($booksas$book){$aut...