PHP upload pictures, automatically cut into thumbnails, wide open _php tutorial

Source: Internet
Author: User
PHP Tutorial After uploading images, automatically cropped to thumbnails, width is not limited to high

$Id: image.php 1937 2009-01-05 19:09:40z Dualface $

/**
* Define 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 2009-01-05 19:09:40z Dualface $
* @package Helper
*/

/**
* The Helper_image class encapsulates the operation for the image
*
* Developers cannot directly construct instances of this class, but should use Helper_image::createfromfile ()
* Static method creates an instance of an Image class.
*
* When working with large images, make sure PHP is able to allocate enough memory.
*
* @author Yulei Liao
* @version $Id: image.php 1937 2009-01-05 19:09:40z Dualface $
* @package Helper
*/
Abstract class Helper_image
{
/**
* Create Helper_imagegd object from specified file
*
Usage
* @code PHP
* $image = helper_image::createfromfile (' 1.jpg ');
* $image->resize ($width, $height);
* $image->saveasjpeg (' 2.jpg ');
* @endcode
*
* For uploaded files, the extension is not included because of its temporary file name.
* Therefore, you need to create an Image object using the following method:
*
* @code PHP
* $ext = pathinfo ($_files[' postfile ' [' name '], pathinfo_extension);
* $image = image::createfromfile ($_files[' postfile ' [' tmp_name '], $ext);
* @endcode
*
* @param string $filename The full path to the image file
* @param string $fileext Specify the extension
*
* @return Helper_imagegd HELPER_IMAGEGD objects created from a file
* @throw q_notimplementedexception
*/
static function CreateFromFile ($filename, $fileext)
{
$fileext = Trim (Strtolower ($fileext), '. ');
$ext 2functions = Array (
' jpg ' = ' imagecreatefromjpeg ',
' jpeg ' = ' imagecreatefromjpeg ',
' png ' = ' imagecreatefrompng ',
' gif ' = ' imagecreatefromgif '
);

if (!isset ($ext 2functions[$fileext]))
{
throw new Q_notimplementedexception (__ (' Imagecreateform '. $fileext));
}

$handle = Call_user_func ($ext 2functions[$fileext], $filename);
return new HELPER_IMAGEGD ($handle);
}

/**
* Converts a 16 binary color value to an RGB value
*
* Usage:
* @code php
* $color = ' #369 ';
* List ($r, $g, $b) = Helper_i Mage::hex2rgb ($color);
* echo "red: {$r}, Green: {$g}, Blue: {$b}";
* @endcode
*
* @param string $color color value
* @param string $default The default color returned when using an invalid color value
*
* @return Arra Y an array of RGB tri-Colors
*/
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 >> & 0xFF, ($dec >> 8) & 0xFF, $dec & 0xff);
}
}

/**
* The Helper_imagegd class encapsulates a GD handle for manipulating images
*
* @author Yulei Liao
* @version $Id: image.php 1937 2009-01-05 19:09:40z Dualface $
* @package Helper
*/
Class Helper_imagegd
{
/**
* GD Resource handle
*
* @var Resource
*/
protected $_handle = null;

/**
* Constructor function
*
* @param resource $handle GD resource handle
*/
function __construct ($handle)
{
$this->_handle = $handle;
}

/**
* destructor
*/
function __destruct ()
{
$this->destroy ();
}

/**
* Quickly scale images to a specified size (poor quality)
*
* @param int $width new width
* @param int $height new height
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for 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;
}

/**
* Zoom image to a specified size (better quality, slower than resize ())
*
* @param int $width new width
* @param int $height new height
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for 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;
}

/**
* Resizing the image without zooming
*
Usage
* @code PHP
* $image->resizecanvas ($width, $height, ' top-left ');
* @endcode
*
* $pos parameter specifies where the image content is aligned when the image is resized.
* The available values for $pos parameters are:
*
*-Left: right-aligned
*-Right: Justified
*-Center: Centre alignment
*-top: Top Align
*-bottom: Bottom alignment
*-Top-left, Left-top: upper left corner aligned
*-Top-right, right-top: Align upper right corner
*-Bottom-left, Left-bottom: Align Left bottom corner
*-Bottom-right, Right-bottom: Align Right bottom corner
*
* If an invalid $pos parameter is specified, it is equivalent to specifying center.
*
* @param int $width new height
* @param int $height new width
* @param string $pos changes in image position when adjusting
* @param the default color of the blank part of string $bgcolor
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for 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);

//Based on the Pos property to determine how to position the original picture
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;
B Reak;
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 image aspect ratio
*
* Crop () maintains the aspect ratio of the image when zooming the image, ensuring that the image does not pull high or flatten.
*
* Crop () The maximum scaling is calculated by default in terms of $width and $height parameters.
* Keep the image of the cut to the fullest extent possible with images.
*
* For example, the size of the source map is 600, while the specified $width and $height are 200 and 100.
* Then the source map will be reduced to a size of x 150, then the extra 50 pixel height is cut off.
*
Usage
* @code PHP
* $image->crop ($width, $height);
* @endcode
*
* If you want the final build picture to always contain the full image content, you should specify $options parameters.
* The available values for this parameter are:
*
*-Fullimage: Whether to keep the full image
*-POS: Alignment when zooming
*-bgcolor: background color of extra part when zooming
*-Enlarge: whether to allow amplification
*-Reduce: Whether to allow Zoom out
*
* Where available values for $options [' POS '] parameters are:
*
*-Left: right-aligned
*-Right: Justified
*-Center: Centre alignment
*-top: Top Align
*-bottom: Bottom alignment
*-Top-left, Left-top: upper left corner aligned
*-Top-right, right-top: Align upper right corner
*-Bottom-left, Left-bottom: Align Left bottom corner
*-Bottom-right, Right-bottom: Align Right bottom corner
*
* If an invalid $pos parameter is specified, it is equivalent to specifying center.
*
* Each of the options in the $options can be specified individually, such as placing the image in the lower-right corner of the new picture when clipping is allowed.
*
* @code PHP
* $image->crop ($width, $height, Array (' pos ' = ' right-bottom '));
* @endcode
*
* @param int $width new width
* @param int $height new height
* @param array $options cut options
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for a coherent interface
*/
function crop ($width, $height, $options = Array ())
{
if (Is_null ($this->_handle)) return $this;

$default _options = Array (
' Fullimage ' = False,
' pos ' = ' center ',
' bgcolor ' = ' 0xfff ',
' Enlarge ' = False,
' Reduce ' = true,
);
$options = Array_merge ($default _options, $options);

Create a target image
$dest = Imagecreatetruecolor ($width, $height);
Fill 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);

Calculates the aspect ratio based on the source graph
$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 [' fullimage '])
{
If you want to maintain 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 [' enlarge '] && $ratio > 1) $ratio = 1;
if (! $options [' Reduce '] && $ratio < 1) $ratio = 1;

Calculate the width height and position of the target area
$DST _w = $full _w * $ratio;
$DST _h = $full _h * $ratio;

//Based on POS properties to determine how to locate
switch (strtolower ($options [' pos]))
{
case ' left ':
$dst _x = 0;
$dst _y = ($hei ght-$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-$d St_w;
$dst _y = 0;
break;
Case ' bottom-left ':
Case ' left-bottom ':
$dst _x = 0;
$dst _y = $height-$dst _h,
Break,
case ' Botto M-right ':
Case ' right-bottom ':
$dst _x = $width-$dst _w,
$dst _y = $height-$dst _h;
break;
Case ' Cente R ':
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 JPEG file
*
* @param string $filename Save file name
* @param int $quality quality parameter, default is 80
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for a coherent interface
*/
function Saveasjpeg ($filename, $quality = 80)
{
Imagejpeg ($this->_handle, $filename, $quality);
}

/**
* Save As PNG file
*
* @param string $filename Save file name
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for a coherent interface
*/
function Saveaspng ($filename)
{
Imagepng ($this->_handle, $filename);
}

/**
* Save As GIF file
*
* @param string $filename Save file name
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for a coherent interface
*/
function Saveasgif ($filename)
{
Imagegif ($this->_handle, $filename);
}

/**
* Destroy images in memory
*
* @return Helper_imagegd Returns the Helper_imagegd object itself for a coherent interface
*/
function Destroy ()
{
if (! $this->_handle)
{
@imagedestroy ($this->_handle);
}
$this->_handle = null;
return $this;
}
}

Calling methods

$image = Helper_image::createfromfile (' c:a.jpg ', ' jpg ');
$image->resampled (100, 100); Zoom to 100px * 100PX
$image->saveasjpeg (' c:a_output.jpg ', 100);

http://www.bkjia.com/PHPjc/632971.html www.bkjia.com true http://www.bkjia.com/PHPjc/632971.html techarticle php Tutorial upload pictures, automatically cut into thumbnails, wide open-ended? PHP//$Id: image.php 1937 2009-01-05 19:09:40z dualface $/** * Define Helper_image classes and Helper_i Magegd class * ...

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