PHP image processing Class (generate thumbnails, add watermarks, get picture information) _php Tutorial

Source: Internet
Author: User
PHP Tutorial Image Processing class (generate thumbnails, add watermarks, get picture information)
This article provides this image processing class, he can do things have the image generated thumbnails, may add watermark to the image and get the picture information, is a more practical code and concise function */

Class image
{
Public $info =array ();

function __construct ()
{
!extension_loaded (' GD ') && exit ("www.bKjia.c0m Hint: the server environment does not support the GD library");
return true;
}

function image ()
{
$this->__construct ();
}

function Thumb ($image, $thumb _width=300, $thumb _height=225)
{
$info = $this->info ($image);
$scale =min (1,min ($thumb _width/$info [' width '], $thumb _height/$info [' height ']); Scale by scaling
$thumb _width=intval ($info [' width ']* $scale);
$thumb _height=intval ($info [' Height ']* $scale);
$createfunc = ' Imagecreatefrom '. ($info [' type ']== ' jpg '? ' JPEG ': $info [' type ']);
$im = $createfunc ($image);
$thumb _im= $info [' type ']!= ' gif ' && function_exists (' Imagecreatetruecolor ')? Imagecreatetruecolor ($thumb _ width, $thumb _height): Imagecreate ($thumb _width, $thumb _height);
Imagecopyresampled ($thumb _im, $im, 0,0,0,0, $thumb _width, $thumb _height, $info [' width '], $info [' height ']);
if ($info [' type ']== ' gif ' | | $info [' type ']== ' png ')
{
$bgcolor =imagecolorallocate ($thumb _im,0,255,0);
Imagecolortransparent ($thumb _im, $bgcolor);
}
$imagefunc = ' image '. ($info [' type ']== ' jpg '? ' JPEG ': $info [' type ']);
$thumbname = ' thumb_ '. $info [' name ']. '. '. $info [' type '];
$imagefunc ($thumb _im, $info [' path ']. $thumbname);
Imagedestroy ($im);
Imagedestroy ($thumb _im);
return $info [' path ']. $thumbname;
}

function watermark ($image, $pos =9, $watermarkimg = ' images/watermark.gif ', $pct =65, $text = ', $w _font=5, $w _color= ' # ff0000 ')
{
$imageinfo = $this->info ($image);
$source _w= $imageinfo [' width '];
$source _h= $imageinfo [' height '];
$imagecreatefunc = ' Imagecreatefrom '. ($imageinfo [' type ']== ' jpg '? ' JPEG ': $imageinfo [' type ']);
$im = $imagecreatefunc ($image);
if (!empty ($watermarkimg) && file_exists ($watermarkimg))//Add Picture watermark
{
$iswaterimage =true;
$watermarkinfo = $this->info ($watermarkimg);
$width = $watermarkinfo [' width '];
$height = $watermarkinfo [' height '];
$watermarkcreatefunc = ' Imagecreatefrom '. ($watermarkinfo [' type ']== ' jpg '? ' JPEG ': $watermarkinfo [' type ']);
$watermark _im= $watermarkcreatefunc ($watermarkimg);
}
else//Add text watermark
{
$iswaterimage =false;
if (!empty ($w _color) && strlen ($w _color) ==7)
{
$r =hexdec (substr ($w _color,1,2));
$g =hexdec (substr ($w _color,3,2));
$b =hexdec (substr ($w _color,5,2));
}
$temp = Imagettfbbox (ceil ($w _font*2.5), 0, ' Fonts/alger.ttf ', $text);
$width = $temp [2]-$temp [6];
$height = $temp [3]-$temp [7];
Unset ($temp);
}
Switch ($POS)
{
Case 0:
$WX = Mt_rand (0, ($source _w-$width));
$wy = Mt_rand (0, ($source _h-$height));
Break
Case 1:
$WX = 5;
$wy = 5;
Break
Case 2:
$WX = ($source _w-$width)/2;
$wy = 5;
Break
Case 3:
$WX = $source _w-$width-5;
$wy = 5;
Break
Case 4:
$WX = 5;
$wy = ($source _h-$height)/2;
Break
Case 5:
$WX = ($source _w-$width)/2;
$wy = ($source _h-$height)/2;
Break
Case 6:
$WX = $source _w-$width-5;
$wy = ($source _h-$height)/2;
Break
Case 7:
$WX = 5;
$wy = $source _h-$height-5;
Break
Case 8:
$WX = ($source _w-$width)/2;
$wy = $source _h-$height-5;
Break
Default
$WX = $source _w-$width-5;
$wy = $source _h-$height-5;
Break
}
if ($iswaterimage)
{
if ($imageinfo [' type '] = = ' png ') {
Imagecopy ($im, $watermark _im, $wx, $wy, 0, 0, $width, $height);
} else {
Imagecopymerge ($im, $watermark _im, $wx, $wy, 0, 0, $width, $height, $pct);
}
}
Else
{
Imagestring ($im, $w _font, $wx, $wy, $text, Imagecolorallocate ($im, $r, $g, $b));
}
$imagefunc = ' image '. ($imageinfo [' type ']== ' jpg '? ' JPEG ': $imageinfo [' type ']);
$imagefunc ($im, $image);
Imagedestroy ($im);
return true;
}

function info ($image)
{
$info =array ();
$info [' Size ']=filesize ($image);
$imageinfo =getimagesize ($image);
$info [' width ']= $imageinfo [0];
$info [' Height ']= $imageinfo [1];
$info [' width_height ']= $imageinfo [3];
$info [' MIME ']= $imageinfo [' MIME '];
Unset ($imageinfo);
$imageinfo =pathinfo ($image);
$info [' Path ']= $imageinfo [' dirname ']. ' /';
$info [' type ']=strtolower ($imageinfo [' extension ']); Picture type, not including '. '
$info [' name ']= $imageinfo [' filename '];
Unset ($imageinfo, $name);
$this->info= $info;
return $info;
}
}

http://www.bkjia.com/PHPjc/633005.html www.bkjia.com true http://www.bkjia.com/PHPjc/633005.html techarticle PHP Tutorial Picture processing class (generate thumbnails, add watermarks, get picture information) This article provides this image processing class, he can do things have to make pictures of thumbnails, may give pictures to increase ...

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