PHP图片等比缩放类SimpleImage使用方法和使用实例分享


Posted in PHP onApril 10, 2014

使用方法示例:
设定宽度,等比例缩放

<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToWidth(250);
   $image->save('picture2.jpg');?>

设定高度,等比例缩放
<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToHeight(500);
   $image->save('picture2.jpg');
   $image->resizeToHeight(200);
   $image->save('picture3.jpg');?>

按比例,缩放至50%
<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->scale(50);
   $image->save('picture2.jpg');?>

缩放后直接输出到屏幕
<?php
   header('Content-Type: image/jpeg');
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToWidth(150);
   $image->output();?>

使用例子:

<?php
include("SimpleImage.php");//图片处理类在下面
$url="http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg";
$picfile = down($url);//下载图片(下载图片的路径可以处理完成后清空,这里未进行处理)
$res = new SimpleImage();//图片处理实例
$res = $res->load($picfile);
$tmpfile = tempfile().'.jpg';//创建一个路径文件用来保存图片
$width = '30';//设定图片的宽度
$res->resizeToWidth($width);
$res->save($tmpfile);//把处理后的图片保存(无.jpg后缀)
//这里总共产生了3个文件,一个是下载的图片文件,一个是临时文件,最后一个是处理的图片文件。需要优化清理掉前两个文件。
 

function down($url)
{
        $http = array();
        $header = "http://f3.v.veimg.cn";
        if ($header) {
            $http['header'] = $header;
        }
        $http['timeout'] = 50;
        $ctx = stream_context_create(array(
            'http' => $http,
        )); 
        $content = @file_get_contents($url, 0, $ctx);
        sleep(1);
        if (!$content) {
            return false;
        }
        $tmpfile = tempfile();
        file_put_contents($tmpfile, $content);
        return $tmpfile;
}
function tempfile()
{
        $path = dirname(__FILE__);
        $path .= '/spider/' . date('Ymd') . '/'.date('His').'-' . (int)(time() / 300);
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
        }
        do {
            $file = $path . '/' . dechex(mt_rand());
        }
        while (file_exists($file));
        touch($file);
        return $file;
}

图片处理类源码(官网地址:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):

<?php/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
class SimpleImage {
    var $image;
    var $image_type;
    function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];
        if( $this->image_type == IMAGETYPE_JPEG ) {
            $this->image = @imagecreatefromjpeg($filename);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            $this->image = @imagecreatefromgif($filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            $this->image = @imagecreatefrompng($filename);
        }
        if (!$this->image) {
            return false;
        }
        return $this;
    }
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
        if( $image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image,$filename,$compression);
        } elseif( $image_type == IMAGETYPE_GIF ) {
            imagegif($this->image,$filename);
        } elseif( $image_type == IMAGETYPE_PNG ) {
            imagepng($this->image,$filename);
        }
        if( $permissions != null) {
            chmod($filename,$permissions);
        }
    }
    function output($image_type=IMAGETYPE_JPEG) {
        if( $image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image);
        } elseif( $image_type == IMAGETYPE_GIF ) {
            imagegif($this->image);
        } elseif( $image_type == IMAGETYPE_PNG ) {
            imagepng($this->image);
        }
    }
    function getWidth() {
        return imagesx($this->image);
    }
    function getHeight() {
        return imagesy($this->image);
    }
    function resizeToHeight($height) {
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width,$height);
    }
    function resizeToWidth($width) {
        if ($this->getWidth() < $width) {
            $width = $this->getWidth();
        }
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width,$height);
    }
    function scale($scale) {
        $width = $this->getWidth() * $scale/100;
        $height = $this->getheight() * $scale/100;
        $this->resize($width,$height);
    }
    function resize($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }
 
    function resize2($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        if( $this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG ) {
            $current_transparent = imagecolortransparent($this->image);
            if($current_transparent != -1) {
                $transparent_color = imagecolorsforindex($this->image, $current_transparent);
                $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
                imagefill($new_image, 0, 0, $current_transparent);
                imagecolortransparent($new_image, $current_transparent);
            } elseif( $this->image_type == IMAGETYPE_PNG) {
                imagealphablending($new_image, false);
                $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
                imagefill($new_image, 0, 0, $color);
                imagesavealpha($new_image, true);
            }
        }
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;   
    }
}
PHP 相关文章推荐
vBulletin HACK----显示话题大小和打开新窗口于论坛索引页
Oct 09 PHP
php中ob(Output Buffer 输出缓冲)函数使用方法
Jul 21 PHP
Discuz 模板语句分析及知识技巧
Aug 21 PHP
PHP自动识别字符集并完成转码详解
Aug 02 PHP
可以保证单词完整性的PHP英文字符串截取代码分享
Jul 15 PHP
PHP图像处理类库MagickWand用法实例分析
May 21 PHP
php计算整个目录大小的方法
Jun 01 PHP
PHP快速推送微信模板消息
Apr 14 PHP
Yii框架ACF(accessController)简单权限控制操作示例
Apr 26 PHP
php菜单/评论数据递归分级算法的实现方法
Aug 01 PHP
laravel自定义分页的实现案例offset()和limit()
Oct 15 PHP
基于thinkphp5框架实现微信小程序支付 退款 订单查询 退款查询操作
Aug 17 PHP
PHP按行读取、处理较大CSV文件的代码实例
Apr 09 #PHP
PHP二维数组排序的3种方法和自定义函数分享
Apr 09 #PHP
php计算几分钟前、几小时前、几天前的几个函数、类分享
Apr 09 #PHP
PHP扩展模块Pecl、Pear以及Perl的区别
Apr 09 #PHP
排序算法之PHP版快速排序、冒泡排序
Apr 09 #PHP
PHP读取大文件的类SplFileObject使用介绍
Apr 09 #PHP
php解决约瑟夫环示例
Apr 09 #PHP
You might like
PHP+Ajax实时自动检测是否联网的方法
2015/07/01 PHP
PHP读取配置文件类实例(可读取ini,yaml,xml等)
2015/07/28 PHP
php+ajax实现仿百度查询下拉内容功能示例
2017/10/20 PHP
jquery和雅虎的yql服务实现天气预报服务示例
2014/02/08 Javascript
js中定义一个变量并判断其是否为空的方法
2014/05/13 Javascript
jQuery遍历之next()、nextAll()方法使用实例
2014/11/08 Javascript
js控制页面的全屏展示和退出全屏显示的方法
2015/03/10 Javascript
JavaScript获得url所有参数键值表的方法
2015/03/21 Javascript
jQuery实现给页面换肤的方法
2015/05/30 Javascript
同步异步动态引入js文件的几种方法总结
2016/09/23 Javascript
Bootstrap页面标题Page Header的实现方法
2017/03/22 Javascript
基于jQuery实现的Ajax 验证用户名唯一性实例代码
2017/06/28 jQuery
vue :src 文件路径错误问题的解决方法
2018/05/15 Javascript
JavaScript基于对象方法实现数组去重及排序操作示例
2018/07/10 Javascript
python实现根据窗口标题调用窗口的方法
2015/03/13 Python
Python遍历目录中的所有文件的方法
2016/07/08 Python
浅谈django开发者模式中的autoreload是如何实现的
2017/08/18 Python
Python中return self的用法详解
2018/07/27 Python
使用EduBlock轻松学习Python编程
2018/10/08 Python
win7 x64系统中安装Scrapy的方法
2018/11/18 Python
python PrettyTable模块的安装与简单应用
2019/01/11 Python
python 多进程队列数据处理详解
2019/12/23 Python
python lambda的使用详解
2021/02/26 Python
世界上最大的网络主机公司:1&1
2016/10/12 全球购物
自我鉴定模板
2013/10/29 职场文书
市场营销毕业生自荐信
2013/11/23 职场文书
设计专业自荐信
2014/06/19 职场文书
四风问题对照检查材料整改措施
2014/09/27 职场文书
2014年应急工作总结
2014/12/11 职场文书
以权谋私检举信范文
2015/03/02 职场文书
公司庆典主持词
2015/07/04 职场文书
教师廉政准则心得体会
2016/01/20 职场文书
2019年幼儿园管理条例范本!
2019/07/17 职场文书
python面向对象版学生信息管理系统
2021/06/24 Python
Python获取指定日期是"星期几"的6种方法
2022/03/13 Python
联想win10摄像头打不开怎么办?win10笔记本摄像头打不开解决办法
2022/04/08 数码科技