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

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

 更新時(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)建一個(gè)路徑文件用來(lái)保存圖片
$width = '30';//設(shè)定圖片的寬度
$res->resizeToWidth($width);
$res->save($tmpfile);//把處理后的圖片保存(無(wú).jpg后綴)
//這里總共產(chǎn)生了3個(gè)文件,一個(gè)是下載的圖片文件,一個(gè)是臨時(shí)文件,最后一個(gè)是處理的圖片文件。需要優(yōu)化清理掉前兩個(gè)文件。

 


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忘記后臺(tái)密碼的解決辦法

    PHPCMS忘記后臺(tái)密碼的解決辦法

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

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

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

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

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

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

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

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

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

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

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

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

    下面小編就為大家?guī)?lái)一篇php實(shí)現(xiàn)當(dāng)前頁(yè)面點(diǎn)擊下載文件的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    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制作的列表分頁(yè)的功能模塊

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

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

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

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

最新評(píng)論

九龙城区| 浦北县| 容城县| 阿坝| 巴里| 兰溪市| 始兴县| 新化县| 桐梓县| 盘山县| 朝阳县| 苏尼特左旗| 同心县| 巫山县| 南昌市| 合肥市| 讷河市| 田东县| 阜康市| 九龙坡区| 岳阳县| 凤阳县| 邳州市| 九台市| 留坝县| 遂川县| 汾西县| 简阳市| 辽宁省| 涟源市| 高台县| 井冈山市| 汉中市| 长岛县| 华池县| 瓦房店市| 托里县| 五河县| 定日县| 武清区| 长岛县|