PHP graphics SimpleImage use method and use instance to share _php instance

Source: Internet
Author: User
Tags imagejpeg

Examples of using methods:
Set width, equal scaling

Copy Code code as follows:

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

Set height, equal proportional scaling
Copy Code 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%
Copy Code code as follows:

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

Output directly to screen after zooming
Copy Code 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 examples:

Copy Code code as follows:

<?php
Include ("simpleimage.php");//The Picture processing class is below

$url = "Yun_qi_img/20130703100937552.jpg";
$picfile = Down ($url);//download picture (the path to download the picture can be processed and emptied, no processing is done here)
$res = new SimpleImage ()//Picture Processing instance
$res = $res->load ($picfile);
$tmpfile = Tempfile (). JPG ';//create a path file to save a picture
$width = ' 30 ';//Set the width of the picture
$res->resizetowidth ($width);
$res->save ($tmpfile);//Save the processed picture (No. jpg suffix)
Here a total of 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;
}

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


Picture processing class source code (official website address: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/):

Copy Code code as follows:

<?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 are 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 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) {

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

}

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.