Php automatically crops an image into a thumbnail after uploading it. the width is not limited to height _ PHP Tutorial

Source: Internet
Author: User
After uploading an image in php, the image is automatically cropped to a thumbnail with no width or height. After uploading an image in the php Tutorial, the image will be automatically cropped to a thumbnail. what is the width and height? Php $ Id: image. php19372009-01-0519: 09: 40 Zdualface $ *** defines the Helper_Image class and Helper_ImageGD class * php Tutorial after uploading an image, automatically crop it to a thumbnail, width is not limited to height

// $ Id: image. php 1937 19: 09: 40Z dualface $

/**
* Defines the Helper_Image class and Helper_ImageGD class.
*
* @ Link http://qeephp.com/
* @ Copyright Copyright (c) 2006-2009 Qeeyuan Inc. {@ link http://www.qeeyuan.com}
* @ License New BSD License {@ link http://qeephp.com/license}
* @ Version $ Id: image. php 1937 19: 09: 40Z dualface $
* @ Package helper
*/

/**
* The Helper_Image class encapsulates operations on images.
*
* Developers cannot directly construct instances of this class, but should use Helper_Image: createFromFile ()
* Create an Image class instance using static methods.
*
* When operating on large images, make sure that php can allocate enough memory.
*
* @ Author YuLei Liao
* @ Version $ Id: image. php 1937 19: 09: 40Z dualface $
* @ Package helper
*/
Abstract class Helper_Image
{
/**
* Create a Helper_ImageGD object from a specified file
*
* Usage:
* @ Code php
* $ Image = Helper_Image: createFromFile('1.jpg ');
* $ Image-> resize ($ width, $ height );
* $ Image-> saveas(('2.jpg ');
* @ Endcode
*
* For uploaded files, the temporary file name does not contain the extension.
* Therefore, you must use the following method to create an Image object:
*
* @ Code php
* $ Ext = pathinfo ($ _ FILES ['postfile'] ['name'], PATHINFO_EXTENSION );
* $ Image = Image: createFromFile ($ _ FILES ['postfile'] ['tmp _ name'], $ ext );
* @ Endcode
*
* @ Param string $ full path of the filename image file
* @ Param string $ fileext specifies the extension
*
* @ Return Helper_ImageGD the Helper_ImageGD object created from the file
* @ Throw Q_NotImplementedException
*/
Static function createFromFile ($ filename, $ fileext)
{
$ Fileext = trim (strtolower ($ fileext ),'.');
$ Ext2functions = array (
'Jpg '=> 'imagecreatefromjpeg ',
'Jpeg '=> 'imagecreatefromjpeg ',
'PNG '=> 'imagecreatefrompng ',
'Gif' => 'imagecreatefromgif'
);

If (! Isset ($ ext2functions [$ fileext])
{
Throw new Q_NotImplementedException (_ ('imagecreateform'. $ fileext ));
}

$ Handle = call_user_func ($ ext2functions [$ fileext], $ filename );
Return new Helper_ImageGD ($ handle );
}

/**
* Convert hexadecimal color values to rgb values
*
* Usage:
* @ Code php
* $ Color = '#369 ';
* List ($ r, $ g, $ B) = Helper_Image: hex2rgb ($ color );
* Echo "red: {$ r}, green: {$ g}, blue: {$ B }";
* @ Endcode
*
* @ Param string $ color value
* @ Param string $ default: the default color returned when invalid color values are used
*
* @ Return array an array composed of three colors: RGB
*/
Static function hex2rgb ($ color, $ default = 'ffffff ')
{
$ Hex = trim ($ color, '# & hh ');
$ Len = strlen ($ hex );
If ($ len = 3)
{
$ Hex = "{$ hex [0]} {$ hex [0]} {$ hex [1]} {$ hex [1]} {$ hex [2]} {$ hex [2]} ";
}
Elseif ($ len <6)
{
$ Hex = $ default;
}
$ Dec = hexdec ($ hex );
Return array ($ dec> 16) & 0xff, ($ dec> 8) & 0xff, $ dec & 0xff );
}
}

/**
* The Helper_ImageGD class encapsulates a gd handle for image operations.
*
* @ Author YuLei Liao
* @ Version $ Id: image. php 1937 19: 09: 40Z dualface $
* @ Package helper
*/
Class Helper_ImageGD
{
/**
* GD resource handle
*
* @ Var resource
*/
Protected $ _ handle = null;

/**
* Constructor
*
* @ Param resource $ handle GD resource handle
*/
Function _ construct ($ handle)
{
$ This-> _ handle = $ handle;
}

/**
* Destructor
*/
Function _ destruct ()
{
$ This-> destroy ();
}

/**
* Quickly scale the image to a specified size (poor quality)
*
* @ Param int $ new width
* @ Param int $ new height
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function resize ($ width, $ height)
{
If (is_null ($ this-> _ handle) return $ this;

$ Dest = imagecreatetruecolor ($ width, $ height );
Imagecopyresized ($ dest, $ this-> _ handle, 0, 0, 0, 0,
$ Width, $ height,
Imagesx ($ this-> _ handle), imagesy ($ this-> _ handle ));
Imagedestroy ($ this-> _ handle );
$ This-> _ handle = $ dest;
Return $ this;
}

/**
* Scale the image to the specified size (the quality is better and the speed is slower than resize)
*
* @ Param int $ new width
* @ Param int $ new height
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function resampled ($ width, $ height)
{
If (is_null ($ this-> _ handle) return $ this;
$ Dest = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ dest, $ this-> _ handle, 0, 0, 0, 0,
$ Width, $ height,
Imagesx ($ this-> _ handle), imagesy ($ this-> _ handle ));
Imagedestroy ($ this-> _ handle );
$ This-> _ handle = $ dest;
Return $ this;
}

/**
* Adjust the image size without scaling
*
* Usage:
* @ Code php
* $ Image-> resizeCanvas ($ width, $ height, 'top-left ');
* @ Endcode
*
* The $ pos parameter specifies the hour when the image size is adjusted and where the image content is aligned.
* $ Available pos parameters include:
*
*-Left: left alignment
*-Right: right-aligned
*-Center: center alignment
*-Top: top alignment
*-Bottom: bottom alignment
*-Top-left, left-top: alignment in the upper left corner
*-Top-right, right-top: alignment in the upper right corner
*-Bottom-left, left-bottom: align in the lower left corner
*-Bottom-right, right-bottom: bottom right corner alignment
*
* If an invalid $ pos parameter is specified, it is equivalent to the specified center.
*
* @ Param int $ new width height
* @ Param int $ new width of height
* @ Param string $ changes in image position when pos is adjusted
* @ Param string $ default color of the blank part of bgcolor
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function resizeCanvas ($ width, $ height, $ pos = 'center', $ bgcolor = '0xffffff ')
{
If (is_null ($ this-> _ handle) return $ this;

$ Dest = imagecreatetruecolor ($ width, $ height );
$ Sx = imagesx ($ this-> _ handle );
$ Sy = imagesy ($ this-> _ handle );

// Determine how to locate the original image based on the pos attribute
Switch (strtolower ($ pos ))
{
Case 'left ':
$ Ox = 0;
$ Oy = ($ height-$ sy)/2;
Break;
Case 'right ':
$ Ox = $ width-$ sx;
$ Oy = ($ height-$ sy)/2;
Break;
Case 'top ':
$ Ox = ($ width-$ sx)/2;
$ Oy = 0;
Break;
Case 'bottom ':
$ Ox = ($ width-$ sx)/2;
$ Oy = $ height-$ sy;
Break;
Case 'top-left ':
Case 'left-top ':
$ Ox = $ oy = 0;
Break;
Case 'top-right ':
Case 'right-top ':
$ Ox = $ width-$ sx;
$ Oy = 0;
Break;
Case 'bottom-left ':
Case 'left-bottom ':
$ Ox = 0;
$ Oy = $ height-$ sy;
Break;
Case 'bottom-right ':
Case 'right-bottom ':
$ Ox = $ width-$ sx;
$ Oy = $ height-$ sy;
Break;
Default:
$ Ox = ($ width-$ sx)/2;
$ Oy = ($ height-$ sy)/2;
}

List ($ r, $ g, $ B) = Helper_Image: hex2rgb ($ bgcolor, '0xffffff ');
$ Bgcolor = imagecolorallocate ($ dest, $ r, $ g, $ B );
Imagefilledrectangle ($ dest, 0, 0, $ width, $ height, $ bgcolor );
Imagecolordeallocate ($ dest, $ bgcolor );

Imagecopy ($ dest, $ this-> _ handle, $ ox, $ oy, 0, 0, $ sx, $ sy );
Imagedestroy ($ this-> _ handle );
$ This-> _ handle = $ dest;

Return $ this;
}

/**
* Cut the image to a specified size while maintaining the aspect ratio.
*
* When zooming an image, crop () can maintain the aspect ratio of the image to ensure that the image is not raised or squashed.
*
* Crop () calculates the maximum scale according to the $ width and $ height parameters by default,
* The cropped image can be filled with images to the maximum extent.
*
* For example, the source image size is 800x600, while the specified $ width and $ height are 200 and 100.
* The source image is first reduced to 200x150, and then the extra 50 pixel height is reduced.
*
* Usage:
* @ Code php
* $ Image-> crop ($ width, $ height );
* @ Endcode
*
* If you want the final generated image to always contain the complete image content, you should specify the $ options parameter.
* Available values of this parameter include:
*
*-Fullimage: whether to keep the full image
*-Pos: alignment mode during scaling
*-Bgcolor: The background color of the excess part during scaling.
*-Enlarge: whether to allow amplification
*-Reduce: whether to allow downgrading
*
* The available values of the $ options ['pos'] parameter are:
*
*-Left: left alignment
*-Right: right-aligned
*-Center: center alignment
*-Top: top alignment
*-Bottom: bottom alignment
*-Top-left, left-top: alignment in the upper left corner
*-Top-right, right-top: alignment in the upper right corner
*-Bottom-left, left-bottom: align in the lower left corner
*-Bottom-right, right-bottom: bottom right corner alignment
*
* If an invalid $ pos parameter is specified, it is equivalent to the specified center.
*
* $ Options can be specified separately for each option. for example, you can place the image in the lower right corner of the new image when you allow cropping.
*
* @ Code php
* $ Image-> crop ($ width, $ height, array ('pos' => 'right-bottom '));
* @ Endcode
*
* @ Param int $ new width
* @ Param int $ new height
* @ Param array $ options cut options
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function crop ($ width, $ height, $ options = array ())
{
If (is_null ($ this-> _ handle) return $ this;

$ Default_options = array (
'Fullage' => false,
'Pos' => 'center ',
'Bgcolor' => '0xfff ',
'Enabled' => false,
'Reduce' => true,
);
$ Options = array_merge ($ default_options, $ options );

// Create the target image
$ Dest = imagecreatetruecolor ($ width, $ height );
// Fill in the background color
List ($ r, $ g, $ B) = Helper_Image: hex2rgb ($ options ['bgcolor'], '0xffffff ');
$ Bgcolor = imagecolorallocate ($ dest, $ r, $ g, $ B );
Imagefilledrectangle ($ dest, 0, 0, $ width, $ height, $ bgcolor );
Imagecolordeallocate ($ dest, $ bgcolor );

// Calculate the aspect ratio based on the source image
$ Full_w = imagesx ($ this-> _ handle );
$ Full_h = imagesy ($ this-> _ handle );
$ Ratio_w = doubleval ($ width)/doubleval ($ full_w );
$ Ratio_h = doubleval ($ height)/doubleval ($ full_h );

If ($ options ['fullage'])
{
// If you want to keep the full image, select the minimum ratio.
$ Ratio = $ ratio_w <$ ratio_h? $ Ratio_w: $ ratio_h;
}
Else
{
// Otherwise, select the maximum ratio.
$ Ratio = $ ratio_w> $ ratio_h? $ Ratio_w: $ ratio_h;
}

If (! $ Options ['enabled'] & $ ratio> 1) $ ratio = 1;
If (! $ Options ['reduce'] & $ ratio <1) $ ratio = 1;

// Calculate the width and height of the target region.
$ Dst_w = $ full_w * $ ratio;
$ Dst_h = $ full_h * $ ratio;

// Determine how to locate the pos attribute
Switch (strtolower ($ options ['pos'])
{
Case 'left ':
$ Dst_x = 0;
$ Dst_y = ($ height-$ dst_h)/2;
Break;
Case 'right ':
$ Dst_x = $ width-$ dst_w;
$ Dst_y = ($ height-$ dst_h)/2;
Break;
Case 'top ':
$ Dst_x = ($ width-$ dst_w)/2;
$ Dst_y = 0;
Break;
Case 'bottom ':
$ Dst_x = ($ width-$ dst_w)/2;
$ Dst_y = $ height-$ dst_h;
Break;
Case 'top-left ':
Case 'left-top ':
$ Dst_x = $ dst_y = 0;
Break;
Case 'top-right ':
Case 'right-top ':
$ Dst_x = $ width-$ dst_w;
$ Dst_y = 0;
Break;
Case 'bottom-left ':
Case 'left-bottom ':
$ Dst_x = 0;
$ Dst_y = $ height-$ dst_h;
Break;
Case 'bottom-right ':
Case 'right-bottom ':
$ Dst_x = $ width-$ dst_w;
$ Dst_y = $ height-$ dst_h;
Break;
Case 'center ':
Default:
$ Dst_x = ($ width-$ dst_w)/2;
$ Dst_y = ($ height-$ dst_h)/2;
}

Imagecopyresampled ($ dest, $ this-> _ handle, $ dst_x, $ dst_y, 0, 0, $ dst_w, $ dst_h, $ full_w, $ full_h );
Imagedestroy ($ this-> _ handle );
$ This-> _ handle = $ dest;

Return $ this;
}

/**
* Save as a JPEG file
*
* @ Param string $ filename save the file name
* @ Param int $ quality parameter. the default value is 80.
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function saveAsJpeg ($ filename, $ quality = 80)
{
Imagejpeg ($ this-> _ handle, $ filename, $ quality );
}

/**
* Save as a PNG file
*
* @ Param string $ filename save the file name
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function saveAsPng ($ filename)
{
Imagepng ($ this-> _ handle, $ filename );
}

/**
* Save as a GIF file
*
* @ Param string $ filename save the file name
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function saveAsGif ($ filename)
{
Imagegif ($ this-> _ handle, $ filename );
}

/**
* Destroy images in memory
*
* @ Return Helper_ImageGD returns the Helper_ImageGD object and implements a coherent interface.
*/
Function destroy ()
{
If (! $ This-> _ handle)
{
@ Imagedestroy ($ this-> _ handle );
}
$ This-> _ handle = null;
Return $ this;
}
}

Call method

$ Image = Helper_Image: createFromFile ('C: a.jpg ', 'jpg ');
$ Image-> resampled (100,100); // scale to 100px * 100PX
$ Image-> saveAsJpeg ('C: a_output.jpg ', 100 );

Why? Php // $ Id: image. php 1937 19: 09: 40Z dualface $/*** defines the Helper_Image class and Helper_ImageGD class *...

Related Article

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.