最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP圖片等比縮放類SimpleImage使用方法和使用實(shí)例分享

 更新時間:2014年04月10日 08:43:44   作者:  
這篇文章主要介紹了PHP圖片等比縮放類SimpleImage使用方法和使用實(shí)例分享,需要的朋友可以參考下

使用方法示例:
設(shè)定寬度,等比例縮放

復(fù)制代碼 代碼如下:

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

設(shè)定高度,等比例縮放
復(fù)制代碼 代碼如下:

<?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%
復(fù)制代碼 代碼如下:

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

縮放后直接輸出到屏幕
復(fù)制代碼 代碼如下:

<?php
   header('Content-Type: image/jpeg');
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToWidth(150);
   $image->output();?>


使用例子:

復(fù)制代碼 代碼如下:

<?php
include("SimpleImage.php");//圖片處理類在下面

$url="http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg";
$picfile = down($url);//下載圖片(下載圖片的路徑可以處理完成后清空,這里未進(jìn)行處理)
$res = new SimpleImage();//圖片處理實(shí)例
$res = $res->load($picfile);
$tmpfile = tempfile().'.jpg';//創(chuàng)建一個路徑文件用來保存圖片
$width = '30';//設(shè)定圖片的寬度
$res->resizeToWidth($width);
$res->save($tmpfile);//把處理后的圖片保存(無.jpg后綴)
//這里總共產(chǎn)生了3個文件,一個是下載的圖片文件,一個是臨時文件,最后一個是處理的圖片文件。需要優(yōu)化清理掉前兩個文件。

 


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;
}


圖片處理類源碼(官網(wǎng)地址:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):

復(fù)制代碼 代碼如下:

<?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;  
    }

}

相關(guān)文章

  • PHPCMS忘記后臺密碼的解決辦法

    PHPCMS忘記后臺密碼的解決辦法

    PHPCMS是一款網(wǎng)站管理軟件,PHPCMS后臺密碼忘記解決辦法,本文主要是從技術(shù)角度去解決的,請細(xì)看正文
    2016-10-10
  • PHP 代碼簡潔之道(小結(jié))

    PHP 代碼簡潔之道(小結(jié))

    這篇文章主要介紹了PHP 代碼簡潔之道(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-10-10
  • smarty模板引擎之配置文件數(shù)據(jù)和保留數(shù)據(jù)

    smarty模板引擎之配置文件數(shù)據(jù)和保留數(shù)據(jù)

    這篇文章主要介紹了smarty模板引擎之配置文件數(shù)據(jù)和保留數(shù)據(jù)的方法,實(shí)例分析了smarty模板引擎配置文件數(shù)據(jù)及獲取數(shù)據(jù)的具體技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • 如何利用http協(xié)議發(fā)布博客園博文評論

    如何利用http協(xié)議發(fā)布博客園博文評論

    這篇文章主要介紹了利用http協(xié)議發(fā)布博客園博文評論的方法,首先,大家要明確給博文提交評論的實(shí)質(zhì)就是通過http協(xié)議服務(wù)器發(fā)送一個post請求,需要的朋友可以參考下
    2015-08-08
  • thinkPHP5框架實(shí)現(xiàn)基于ajax的分頁功能示例

    thinkPHP5框架實(shí)現(xiàn)基于ajax的分頁功能示例

    這篇文章主要介紹了thinkPHP5框架實(shí)現(xiàn)基于ajax的分頁功能,結(jié)合實(shí)例形式分析了thinkPHP5框架上進(jìn)行ajax分頁操作的具體步驟、實(shí)現(xiàn)代碼與相關(guān)操作方法,需要的朋友可以參考下
    2018-06-06
  • CI框架中zip類應(yīng)用示例

    CI框架中zip類應(yīng)用示例

    CI框架自帶的zip類簡單實(shí)用,本文就來簡單說一下ci框架的zip類的使用,需要的朋友可以參考下
    2014-06-06
  • php實(shí)現(xiàn)當(dāng)前頁面點(diǎn)擊下載文件的實(shí)例代碼

    php實(shí)現(xiàn)當(dāng)前頁面點(diǎn)擊下載文件的實(shí)例代碼

    下面小編就為大家?guī)硪黄猵hp實(shí)現(xiàn)當(dāng)前頁面點(diǎn)擊下載文件的實(shí)例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • thinkphp循環(huán)結(jié)構(gòu)用法實(shí)例

    thinkphp循環(huán)結(jié)構(gòu)用法實(shí)例

    這篇文章主要介紹了thinkphp循環(huán)結(jié)構(gòu)用法,以實(shí)例形式講解了for、volist及foreach的用法,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-11-11
  • 推薦一款PHP+jQuery制作的列表分頁的功能模塊

    推薦一款PHP+jQuery制作的列表分頁的功能模塊

    作者寫博目的是記錄開發(fā)過程,積累經(jīng)驗(yàn),便于以后工作參考。本文主要是記錄了制作PHP+jQuery 支持 url 分頁 / ajax 分頁 的列表分頁類的過程,有需要的朋友可以參考下
    2014-10-10
  • 淺談laravel orm 中的一對多關(guān)系 hasMany

    淺談laravel orm 中的一對多關(guān)系 hasMany

    今天小編就為大家分享一篇淺談laravel orm 中的一對多關(guān)系 hasMany,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論

泗阳县| 海丰县| 新乡市| 北安市| 乾安县| 天等县| 息烽县| 锡林浩特市| 巴马| 于都县| 仁化县| 盐源县| 米易县| 临沧市| 阿拉尔市| 育儿| 邮箱| 两当县| 丽江市| 图木舒克市| 金溪县| 宜阳县| 吉林市| 米泉市| 凤庆县| 诏安县| 塘沽区| 大足县| 建湖县| 怀仁县| 德钦县| 卢湾区| 论坛| 上林县| 永安市| 磴口县| 峨眉山市| 肇源县| 黔江区| 友谊县| 东丽区|