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编程最快明白》第三讲:php数组
Nov 01 PHP
php 文件缓存函数
Oct 08 PHP
Php中文件下载功能实现超详细流程分析
Jun 13 PHP
php number_format() 函数通过千位分组来格式化数字的实现代码
Aug 06 PHP
获取URL文件名后缀
Oct 24 PHP
去掉destoon资讯内容页keywords关键字自带的文章标题的方法
Aug 21 PHP
php实现扫描二维码根据浏览器类型访问不同下载地址
Oct 15 PHP
php+mysql实现简单的增删改查功能
Jul 13 PHP
curl 出现错误的调试方法(必看)
Feb 13 PHP
Yii2语言国际化自动配置详解
Aug 22 PHP
php实例化一个类的具体方法
Sep 19 PHP
TP - 比RBAC更好的权限认证方式(Auth类认证)
Mar 09 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/06/05 PHP
php创建基本身份认证站点的方法详解
2013/06/08 PHP
php遍历解析xml字符串的方法
2016/05/05 PHP
详解thinkphp中的volist标签
2018/01/15 PHP
使用Git实现Laravel项目的自动化部署
2019/11/24 PHP
jQuery asp.net 用json格式返回自定义对象
2010/04/07 Javascript
JS中处理与当前时间间隔的函数代码
2012/05/23 Javascript
treepanel动态加载数据实现代码
2012/12/15 Javascript
Js冒泡事件详解及阻止示例
2014/03/21 Javascript
jQuery使用hide方法隐藏元素自身用法实例
2015/03/30 Javascript
如何利用AngularJS打造一款简单Web应用
2015/12/05 Javascript
详解AngularJS中$http缓存以及处理多个$http请求的方法
2016/02/06 Javascript
Bootstrap table使用方法总结
2017/05/10 Javascript
react组件从搭建脚手架到在npm发布的步骤实现
2019/01/09 Javascript
JS使用对象的defineProperty进行变量监控操作示例
2019/02/02 Javascript
JavaScript动画实例之粒子文本的实现方法详解
2020/07/28 Javascript
Python获取脚本所在目录的正确方法
2014/04/15 Python
Linux 下 Python 实现按任意键退出的实现方法
2016/09/25 Python
python3中set(集合)的语法总结分享
2017/03/24 Python
Python进阶之递归函数的用法及其示例
2018/01/31 Python
python实现媒体播放器功能
2018/02/11 Python
对numpy中的transpose和swapaxes函数详解
2018/08/02 Python
解决pycharm安装后代码区不能编辑的问题
2018/10/28 Python
Python之循环结构
2019/01/15 Python
seek引发的python文件读写的问题及解决
2019/07/26 Python
pytorch 归一化与反归一化实例
2019/12/31 Python
详细分析Python collections工具库
2020/07/16 Python
Html5页面在微信端的分享的实现方法
2018/08/30 HTML / CSS
AmazeUI 缩略图的实现示例
2020/08/18 HTML / CSS
无谷物狗粮:Pooch & Mutt
2018/05/23 全球购物
潘多拉珠宝英国官方网上商店:PANDORA英国
2018/06/12 全球购物
经济贸易系毕业生求职信
2014/05/31 职场文书
国际商务专业求职信
2014/07/15 职场文书
工作收入证明范本
2015/06/12 职场文书
MongoDB安装使用并实现Python操作数据库
2021/06/28 MongoDB
Python 读取千万级数据自动写入 MySQL 数据库
2022/06/28 Python