使用方法示例:
设定宽度,等比例缩放
<?php<BR> include('SimpleImage.php');<BR> $image = new SimpleImage();<BR> $image->load('picture.jpg');<BR> $image->resizeToWidth(250);<BR> $image->save('picture2.jpg');?>
设定高度,等比例缩放
<?php<BR> include('SimpleImage.php');<BR> $image = new SimpleImage();<BR> $image->load('picture.jpg');<BR> $image->resizeToHeight(500);<BR> $image->save('picture2.jpg');<BR> $image->resizeToHeight(200);<BR> $image->save('picture3.jpg');?>
按比例,缩放至50%
<?php<BR> include('SimpleImage.php');<BR> $image = new SimpleImage();<BR> $image->load('picture.jpg');<BR> $image->scale(50);<BR> $image->save('picture2.jpg');?>
缩放后直接输出到屏幕
<?php<BR> header('Content-Type: image/jpeg');<BR> include('SimpleImage.php');<BR> $image = new SimpleImage();<BR> $image->load('picture.jpg');<BR> $image->resizeToWidth(150);<BR> $image->output();?><BR>
使用例子:
<?php<BR>include("SimpleImage.php");//图片处理类在下面</P><P>$url="http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg";<BR>$picfile = down($url);//下载图片(下载图片的路径可以处理完成后清空,这里未进行处理)<BR>$res = new SimpleImage();//图片处理实例<BR>$res = $res->load($picfile);<BR>$tmpfile = tempfile().'.jpg';//创建一个路径文件用来保存图片<BR>$width = '30';//设定图片的宽度<BR>$res->resizeToWidth($width);<BR>$res->save($tmpfile);//把处理后的图片保存(无.jpg后缀)<BR>//这里总共产生了3个文件,一个是下载的图片文件,一个是临时文件,最后一个是处理的图片文件。需要优化清理掉前两个文件。</P><P> </P><P><BR>function down($url)<BR>{<BR> $http = array();<BR> $header = "http://f3.v.veimg.cn";<BR> if ($header) {<BR> $http['header'] = $header;<BR> }</P><P> $http['timeout'] = 50;</P><P> $ctx = stream_context_create(array(<BR> 'http' => $http,<BR> )); <BR> $content = @file_get_contents($url, 0, $ctx);<BR> sleep(1);</P><P> if (!$content) {<BR> return false;<BR> }</P><P> $tmpfile = tempfile();</P><P> file_put_contents($tmpfile, $content);</P><P> return $tmpfile;<BR>}</P><P>function tempfile()<BR>{<BR> $path = dirname(__FILE__);<BR> $path .= '/spider/' . date('Ymd') . '/'.date('His').'-' . (int)(time() / 300);</P><P> if (!file_exists($path)) {<BR> mkdir($path, 0777, true);<BR> }</P><P> do {<BR> $file = $path . '/' . dechex(mt_rand());<BR> }<BR> while (file_exists($file));</P><P> touch($file);</P><P> return $file;<BR>}<BR>
图片处理类源码(官网地址:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):
<?php<br><br>/*<BR>* File: SimpleImage.php<BR>* Author: Simon Jarvis<BR>* Copyright: 2006 Simon Jarvis<BR>* Date: 08/11/06<BR>* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php<BR>*<BR>* This program is free software; you can redistribute it and/or<BR>* modify it under the terms of the GNU General Public License<BR>* as published by the Free Software Foundation; either version 2<BR>* of the License, or (at your option) any later version.<BR>*<BR>* This program is distributed in the hope that it will be useful,<BR>* but WITHOUT ANY WARRANTY; without even the implied warranty of<BR>* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<BR>* GNU General Public License for more details:<BR>* http://www.gnu.org/licenses/gpl.html<BR>*<BR>*/<br><br>class SimpleImage {<br><br> var $image;<BR> var $image_type;<br><br> function load($filename) {<br><br> $image_info = getimagesize($filename);<BR> $this->image_type = $image_info[2];<BR> if( $this->image_type == IMAGETYPE_JPEG ) {<br><br> $this->image = @imagecreatefromjpeg($filename);<BR> } elseif( $this->image_type == IMAGETYPE_GIF ) {<br><br> $this->image = @imagecreatefromgif($filename);<BR> } elseif( $this->image_type == IMAGETYPE_PNG ) {<br><br> $this->image = @imagecreatefrompng($filename);<BR> }<br><br> if (!$this->image) {<BR> return false;<BR> }<br><br> return $this;<BR> }<br><br> function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {<br><br> if( $image_type == IMAGETYPE_JPEG ) {<BR> imagejpeg($this->image,$filename,$compression);<BR> } elseif( $image_type == IMAGETYPE_GIF ) {<br><br> imagegif($this->image,$filename);<BR> } elseif( $image_type == IMAGETYPE_PNG ) {<br><br> imagepng($this->image,$filename);<BR> }<BR> if( $permissions != null) {<br><br> chmod($filename,$permissions);<BR> }<BR> }<BR> function output($image_type=IMAGETYPE_JPEG) {<br><br> if( $image_type == IMAGETYPE_JPEG ) {<BR> imagejpeg($this->image);<BR> } elseif( $image_type == IMAGETYPE_GIF ) {<br><br> imagegif($this->image);<BR> } elseif( $image_type == IMAGETYPE_PNG ) {<br><br> imagepng($this->image);<BR> }<BR> }<BR> function getWidth() {<br><br> return imagesx($this->image);<BR> }<BR> function getHeight() {<br><br> return imagesy($this->image);<BR> }<BR> function resizeToHeight($height) {<br><br> $ratio = $height / $this->getHeight();<BR> $width = $this->getWidth() * $ratio;<BR> $this->resize($width,$height);<BR> }<br><br> function resizeToWidth($width) {<BR> if ($this->getWidth() < $width) {<BR> $width = $this->getWidth();<BR> }<BR> $ratio = $width / $this->getWidth();<BR> $height = $this->getheight() * $ratio;<BR> $this->resize($width,$height);<BR> }<br><br> function scale($scale) {<BR> $width = $this->getWidth() * $scale/100;<BR> $height = $this->getheight() * $scale/100;<BR> $this->resize($width,$height);<BR> }<br><br> function resize($width,$height) {<BR> $new_image = imagecreatetruecolor($width, $height)<b style="color:transparent">来&源gao@dai!ma.com搞$代^码%网</b><img>搞gaodaima代码</img>;<BR> imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());<BR> $this->image = $new_image;<BR> }<br><br> <BR> function resize2($width,$height) {<BR> $new_image = imagecreatetruecolor($width, $height);<BR> if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {<BR> $current_transparent = imagecolortransparent($this->image);<BR> if($current_transparent != -1) {<BR> $transparent_color = imagecolorsforindex($this->image, $current_transparent);<BR> $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);<BR> imagefill($new_image, 0, 0, $current_transparent);<BR> imagecolortransparent($new_image, $current_transparent);<BR> } elseif( $this->image_type == IMAGETYPE_PNG) {<BR> imagealphablending($new_image, false);<BR> $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);<BR> imagefill($new_image, 0, 0, $color);<BR> imagesavealpha($new_image, true);<BR> }<BR> }<BR> imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());<BR> $this->image = $new_image; <BR> }<br><br>}<BR>