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的substr_replace将指定两位置之间的字符替换为*号
May 04 PHP
php 中英文语言转换类代码
Aug 11 PHP
介绍一些PHP判断变量的函数
Apr 24 PHP
php数据库配置文件一般做法分享
Jul 07 PHP
Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
Jun 19 PHP
php使用$_POST或$_SESSION[]向js函数传参
Sep 16 PHP
php中in_array函数用法分析
Nov 15 PHP
php读取文件内容的方法汇总
Jan 24 PHP
百度工程师讲PHP函数的实现原理及性能分析(三)
May 13 PHP
php采集神器cURL使用方法详解
Feb 19 PHP
PHP互换两个变量值的方法(不用第三变量)
Nov 14 PHP
php7 list()、session及其他模块的修改实例分析
May 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
PHP页面间传递参数实例代码
2008/06/05 PHP
php Memcache 中实现消息队列
2009/11/24 PHP
限制ckeditor上传图片文件大小的方法
2013/11/15 PHP
codeigniter数据库操作函数汇总
2014/06/12 PHP
Opcache导致php-fpm崩溃nginx返回502
2015/03/02 PHP
使用Modello编写JavaScript类
2006/12/22 Javascript
JavaScript DOM 学习第三章 内容表格
2010/02/19 Javascript
jquery选择器原理介绍($()使用方法)
2014/03/25 Javascript
JavaScript中的call方法和apply方法使用对比
2015/08/12 Javascript
再JavaScript的jQuery库中编写动画效果的指南
2015/08/13 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记9)
2015/12/24 Javascript
Nodejs如何复制文件
2016/03/09 NodeJs
jQuery实现的自定义弹出层效果实例详解
2016/09/04 Javascript
javascript阻止事件冒泡和浏览器的默认行为
2017/01/21 Javascript
学习使用Bootstrap栅格系统
2017/05/11 Javascript
JavaScript实现获取用户单击body中所有A标签内容的方法
2017/06/05 Javascript
nodejs项目windows下开机自启动的方法
2017/11/22 NodeJs
详解node.js 下载图片的 2 种方式
2018/03/02 Javascript
Bootstrap导航菜单点击后无法自动添加active的处理方法
2018/08/10 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
Vue3 中的数据侦测的实现
2019/10/09 Javascript
Python的批量远程管理和部署工具Fabric用法实例
2015/01/23 Python
详解python脚本自动生成需要文件实例代码
2017/02/04 Python
Python多进程multiprocessing用法实例分析
2017/08/18 Python
html5指南-1.html5全局属性(html5 global attributes)深入理解
2013/01/07 HTML / CSS
HTML5标签使用方法详解
2015/11/27 HTML / CSS
LivingSocial英国:英国本地优惠
2019/02/22 全球购物
The Athlete’s Foot新西兰:新西兰最大的运动鞋零售商
2019/12/23 全球购物
英语演讲稿范文
2014/01/03 职场文书
无毒社区工作方案
2014/05/23 职场文书
2014年化妆品销售工作总结
2014/12/01 职场文书
道歉的话语大全
2015/05/12 职场文书
婚庆公司开业主持词
2015/06/30 职场文书
2016年推广普通话宣传周活动总结
2016/04/06 职场文书
CSS3实现模糊背景的三种效果示例
2021/03/30 HTML / CSS
JS不要再到处使用绝对等于运算符了
2021/04/30 Javascript