PHP picture and so on than scaling class SimpleImage use method and use instance to share

Source: Internet
Author: User
Tags imagejpeg

  This article mainly introduces PHP pictures, such as scaling class SimpleImage use methods and use of instance sharing, need friends can refer to the following

Usage Example: Set width, proportional scaling     code as follows: <?php    include (' simpleimage.php ');     $image = new SimpleImage ();     $image->load (' picture.jpg ');     $image->resizetowidth (250);     $image->save (' picture2.jpg ');? > Set height, proportional scaling   code as follows: <?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 ');? > Scale to 50%   code as follows: <?php    include (' simpleimage.php ');     $image = new SimpleImage ();     $image->load (' picture.jpg ');     $image->scale (50);     $image->save (' picture2.jpg ');? > Zoom Direct output to screen   code as follows: <?php    header (' content-type:image/jpeg ');    include (' simpleimage.php ');     $imaGE = new SimpleImage ();     $image->load (' picture.jpg ');     $image->resizetowidth (150);     $image->output ();? >       Use example: The   code is as follows: <?php include ("simpleimage.php");//Picture processing class in the following   $url = " 20130703100937552.jpg "; $picfile = Down ($url);//download picture (the path to download the picture can be processed and emptied after completion, here is not processed) $res = new SimpleImage ()/Picture processing Instance $res = $res->load ($picfile) ; $tmpfile = Tempfile (). JPG ';//create a path file to save the picture $width = ' 30 ';//Set the width of the picture $res->resizetowidth ($width); $res->save ($tmpfile);//Save the processed picture (No. jpg suffix)//The total produced 3 files, one is downloaded picture files, one is temporary files, the last one is the processing of picture files. Needs to be optimized to clean up the first two files.         function down ($url) {        $http = array ();         $header = "http://f3.v.veimg.cn";         if ($header) {            $http [' header '] = $header;   &NB Sp    }           $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;         image processing source code (website address: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):   Code as follows: <?php  /* File:SimpleImage.php * Author:simon Jarvis * copyright:2006 Simon Jarvis * DATE:08/1 1/06 * link:http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php * * * is free software; Can redistribute it and/or * modify it under the terms of the GNU general public License * as published by the Ftware Foundation; Either version 2 * of the License, or (at your option) any later version. * * This are distributed in the hope that it'll be useful, * but without any WARRANTY; Without even the implied warranty of * merchantability or FITNESS for A particular purpose. 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) {            &NBSP ; $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) {    &NBSp       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) {              IM Agegif ($this->image, $filename);        } elseif ($image _type = = imagetype_png) {              IM Agepng ($this->image, $filename);        }         if ($permissions!= null) {          & nbsp   chmod ($filename, $permissions);        }         function output ($image _type=imagetype_jpeg) {&NBsp         if ($image _type = imagetype_jpeg) {            imagejpeg ($thi S->image);        } elseif ($image _type = = imagetype_gif) {              IM Agegif ($this->image);        } elseif ($image _type = = imagetype_png) {              IM Agepng ($this->image);        }     {    function getwidth () {          Retur N Imagesx ($this->image);    }     function getheight () {          return Imagesy ($this->image); nbsp  }     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 = imagecreat Etruecolor ($width, $height);         imagecopyresampled ($new _image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidt H (), $this->getheight ());   &NBsp     $this->image = $new _image;    }         function resize2 ($width, $height) {        $new _image = IM Agecreatetruecolor ($width, $height);         if ($this->image_type = = Imagetype_gif | | $this->image_type = = imagetype_png) {            $current _transparent = imagecolortransparent ($this->image);             if ($current _transparent!=-1) {            &NBSP ;   $transparent _color = Imagecolorsforindex ($this->image, $current _transparent);                 $current _transparent = imagecolorallocate ($new _image, $transparen t_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) {        & nbsp       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;       }  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.