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 表单验证实现代码
Mar 10 PHP
memcached 和 mysql 主从环境下php开发代码详解
May 16 PHP
一些php技巧与注意事项分析
Feb 03 PHP
迅速确定php多维数组的深度的方法
Jan 07 PHP
php实现的Cookies操作类实例
Sep 24 PHP
php中smarty变量修饰用法实例分析
Jun 11 PHP
Ajax PHP JavaScript MySQL实现简易无刷新在线聊天室
Aug 17 PHP
浅谈PHP命令执行php文件需要注意的问题
Dec 16 PHP
thinkphp下MySQL数据库读写分离代码剖析
Apr 18 PHP
PHP编程计算日期间隔天数的方法
Apr 26 PHP
详解PHP中的 input属性(隐藏 只读 限制)
Aug 14 PHP
基于PHP实现短信验证码发送次数限制
Jul 11 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 变量定义和变量替换的方法
2009/07/30 PHP
thinkphp视图模型查询提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解决方法
2014/10/30 PHP
yii2.0整合阿里云oss删除单个文件的方法
2017/09/19 PHP
JavaScript 组件之旅(一)分析和设计
2009/10/28 Javascript
解析Jquery取得iframe中元素的几种方法
2013/07/04 Javascript
JS操作数据库的实例代码
2013/10/17 Javascript
javascript阻止scroll事件多次执行的思路及实现
2013/11/08 Javascript
Jquery 切换不同图片示例代码
2013/12/05 Javascript
javascript中的原型链深入理解
2014/02/24 Javascript
7个JS基础知识总结
2014/03/05 Javascript
jquery操作checkbox实现全选和取消全选
2014/05/02 Javascript
一个用jquery写的判断div滚动条到底部的方法【推荐】
2016/04/29 Javascript
jQuery无刷新上传之uploadify简单代码
2017/01/17 Javascript
微信小程序 出现47001 data format error原因解决办法
2017/03/10 Javascript
jQuery使用bind函数实现绑定多个事件的方法
2017/10/11 jQuery
webpack项目轻松混用css module的方法
2018/06/12 Javascript
jQuery添加新内容的四个常用方法分析【append,prepend,after,before】
2019/03/19 jQuery
JavaScript实现页面高亮操作提示和蒙板
2021/01/04 Javascript
Python 数据结构之旋转链表
2017/02/25 Python
Python实现字符串逆序输出功能示例
2017/06/24 Python
python3.x 生成3维随机数组实例
2019/11/28 Python
django-利用session机制实现唯一登录的例子
2020/03/16 Python
全面解析HTML5中的标准属性与自定义属性
2016/02/18 HTML / CSS
配置H5的滚动条样式的示例代码
2018/03/09 HTML / CSS
MADE法国:提供原创设计师家具
2018/09/18 全球购物
工程专业求职自荐书范文
2014/02/08 职场文书
大学新生入学教育方案
2014/05/16 职场文书
历史学专业求职信
2014/06/19 职场文书
社区综治宣传月活动总结
2014/07/02 职场文书
2015年大学生党员承诺书
2015/04/27 职场文书
高一数学教学反思
2016/02/18 职场文书
MySQL 使用索引扫描进行排序
2021/06/20 MySQL
Windows11里微软已经将驱动程序安装位置A盘删除
2021/11/21 数码科技
Jpa Specification如何实现and和or同时使用查询
2021/11/23 Java/Android
win10+RTX3050ti+TensorFlow+cudn+cudnn配置深度学习环境的方法
2022/06/25 Servers
Python TypeError: ‘float‘ object is not subscriptable错误解决
2022/12/24 Python