PHP image proportional scaling SimpleImage usage and instance sharing _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP image proportional scaling SimpleImage usage and instance sharing. Example code: set the width and proportional scaling copy :? Phpinclude(simpleimage.php?##imagenewsimpleimage(###image-load(picture.jpg); $ image-resi Usage example:
Set width and proportional scaling

The code is as follows:


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


Set height, proportional scaling

The 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 ');?>


Scale to 50% in proportion

The code is as follows:


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


Scale and output directly to the screen

The code is as follows:


Header ('content-Type: image/jpeg ');
Include ('simpleimage. php ');
$ Image = new SimpleImage ();
$ Image-> load('picture.jpg ');
$ Image-> resizeToWidth (150 );
$ Image-> output ();?>


Example:

The code is as follows:


Include ("SimpleImage. php"); // The image processing class is shown below

$ Url = "http://f3.v.veimg.cn/meadincms/1/2013/0703/20130703100937552.jpg ";
$ Picfile = down ($ url); // download the image (the path of the downloaded image can be cleared after processing is completed, but not processed here)
$ Res = new SimpleImage (); // image processing instance
$ Res = $ res-> load ($ picfile );
$ Tmpfile = tempfile().'.jpg '; // create a path file to save the image
$ Width = '30'; // you can specify the image width.
$ Res-> resizeToWidth ($ width );
$ Res-> save ($ tmpfile); // save the processed image (no. JPG suffix)
// Three files are generated here. one is the downloaded image file, the other is the temporary file, and the other is the processed image file. You need to optimize and clear 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 ('ymmd '). '/'. 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 website address: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php ):

The 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 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
* MERCHANTABILITY or fitness for a special PURPOSE. See
* 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, and 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;
}

}

The following code is used to set the width and proportional scaling of a scale :? Php include ('simpleimage. php'); $ image = new SimpleImage (); your 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.