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 相关文章推荐
坏狼的PHP学习教程之第1天
Jun 15 PHP
PHP开发中常用的三个表单验证函数使用小结
Mar 03 PHP
php小偷相关截取函数备忘
Nov 28 PHP
解决PhpMyAdmin中导入2M以上大文件限制的方法分享
Jun 06 PHP
php遍历CSV类实例
Apr 14 PHP
在CentOS上搭建LAMP+vsftpd环境的简单指南
Aug 01 PHP
PHP实现递归目录的5种方法
Oct 27 PHP
php中strlen和mb_strlen用法实例分析
Nov 12 PHP
PHP连接MySQL进行增、删、改、查操作
Feb 19 PHP
Smarty3配置及入门语法
Feb 22 PHP
使用YII2框架实现微信公众号中表单提交功能
Sep 04 PHP
PHPStorm2020.1永久激活及下载更新至2020(推荐)
Sep 25 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
fleaphp crud操作之find函数的使用方法
2011/04/23 PHP
探讨PHP使用eAccelerator的API开发详解
2013/06/09 PHP
基于php和mysql的简单的dao类实现crud操作功能
2014/01/27 PHP
js的一些常用方法小结
2011/06/29 Javascript
JavaScript的常见兼容问题及相关解决方法(chrome/IE/firefox)
2013/12/31 Javascript
jquery text()方法取标签中的文本
2014/07/25 Javascript
JavaScript针对网页节点的增删改查用法实例
2015/02/02 Javascript
JavaScript制作淘宝星级评分效果的思路
2020/06/23 Javascript
基于Javascript实现返回顶部按钮
2016/02/29 Javascript
jQuery实现页面评论栏中访客信息自动填写功能的方法
2016/05/23 Javascript
AngularJS基础 ng-list 指令详解及示例代码
2016/08/02 Javascript
完美解决jQuery的hover事件在IE中不停闪动的问题
2017/02/10 Javascript
Bootstrap多级菜单的实现代码
2017/05/23 Javascript
bootstrap table sum总数量统计实现方法
2017/10/29 Javascript
解决angularjs中同步执行http请求的方法
2018/08/13 Javascript
Vue 中可以定义组件模版的几种方式
2019/08/06 Javascript
对于Python的Django框架部署的一些建议
2015/04/09 Python
python3 selenium 切换窗口的几种方法小结
2018/05/21 Python
python遍历小写英文字母的方法
2019/01/02 Python
Python3爬楼梯算法示例
2019/03/04 Python
Python 使用list和tuple+条件判断详解
2019/07/30 Python
Python实现将蓝底照片转化为白底照片功能完整实例
2019/12/13 Python
pycharm激活码快速激活及使用步骤
2020/03/12 Python
CSS3图片旋转特效(360/60/-360度)
2013/10/10 HTML / CSS
英国第一的滑雪服装和装备零售商:Snow+Rock
2020/02/01 全球购物
为什么如下的代码int a=100,b=100;long int c=a * b;不能工作
2013/11/29 面试题
公务员培训自我鉴定
2013/09/19 职场文书
师范学院教师自荐书
2014/01/31 职场文书
任命书格式
2014/06/05 职场文书
党的群众路线对照检查材料范文
2014/09/24 职场文书
2015年初中元旦晚会活动总结
2014/11/28 职场文书
2014年平安夜寄语
2014/12/08 职场文书
还款承诺书范本
2015/01/20 职场文书
2015年世界环境日活动方案
2015/05/05 职场文书
推销搭讪开场白
2015/05/28 职场文书
JavaScript中的LHS和RHS分析详情
2022/04/06 Javascript