PHP pictures and more than scaling class simpleimage usage and use instances to share _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Examples of Use methods:
Set width, equal scale
Copy CodeThe code is as follows:
Include (' simpleimage.php ');
$image = new SimpleImage ();
$image->load (' picture.jpg ');
$image->resizetowidth (250);
$image->save (' picture2.jpg ');? >
Set height, equal scale
Copy CodeThe code is as follows:
Include (' simpleimage.php ');
$image = new SimpleImage ();
$image->load (' picture.jpg ');
$image->resizetoheight (500);
$image->save (' picture2.jpg ');
$image->resizetoheight (200);
$image->save (' picture3.jpg ');? >
Proportionally, zoom to 50%
Copy CodeThe code is as follows:
Include (' simpleimage.php ');
$image = new SimpleImage ();
$image->load (' picture.jpg ');
$image->scale (50);
$image->save (' picture2.jpg ');? >
Direct output to screen after zooming
Copy CodeThe code is as follows:
Header (' Content-type:image/jpeg ');
Include (' simpleimage.php ');
$image = new SimpleImage ();
$image->load (' picture.jpg ');
$image->resizetowidth (150);
$image->output ();? >


Examples of Use:
Copy the Code code as follows:
Include ("simpleimage.php");//Picture processing class is below

$url = "Http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg";
$picfile = Down ($url);//download picture (the path of the downloaded image can be emptied after processing is done, not processed here)
$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)
A total of 3 files are produced here, one is the downloaded picture file, one is the temporary file, and the last one is the processing picture file. The top two files need to be optimized for cleanup.


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


Image processing class source code (official address: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):
Copy CodeThe code is as follows:

/*
* 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 was 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 was 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. See the
* GNU general public License-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;
}

}

http://www.bkjia.com/PHPjc/751937.html www.bkjia.com true http://www.bkjia.com/PHPjc/751937.html techarticle Example of use: Set width, equal scale copy code code as follows: PHP include (' simpleimage.php '); $image = new SimpleImage (); $image-load (' picture.jpg ' ); $image-resi ...

  • 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.