PHP image Processing

Source: Internet
Author: User
Tags imagejpeg

Synthetic picture (watermark) function Img_water_mark ($SRCIMG, $WATERIMG, $savename, $savepath = ' Public/images/wxqrcode ', $positon = 3, $ ALPHA=100) {//$savename =time ().    JPG ';      $temp = PathInfo ($SRCIMG);      $name = $temp [' basename '];      $path = $temp [' dirname '];      $exte = $temp [' extension ']; $savename = $savename?      $savename: $name; $savepath = $savepath?      $savepath: $path; $savefile = $savepath. ' /'.      $savename;      $srcinfo = @getimagesize ($SRCIMG);  if (! $srcinfo) {return-1;      The original file does not exist} $waterinfo = @getimagesize ($WATERIMG);  if (! $waterinfo) {return-2;      Watermark picture does not exist} $SRCIMGOBJ = Image_create_from_ext ($SRCIMG);  if (! $srcImgObj) {return-3;      The original file image object failed to build} $WATERIMGOBJ = Image_create_from_ext ($WATERIMG);  if (! $waterImgObj) {return-4;      Watermark file Image object failed to build} switch ($positon) {//1 top left Case 1: $x = $y =0; 2 top Right Case 2: $x = $srcinfo [0]-$waterinfo [0]; $y =0;      Break 3 Center Case 3: $x = ($srcinfo [0]-$waterinfo [0])/2; $y = ($srcinfo [1]-$waterinfo [1])/2;      Break 4 bottom left case 4: $x = 0; $y = $srcinfo [1]-$waterinfo [1];      Break 5 bottom Right Case 5: $x = $srcinfo [0]-$waterinfo [0]; $y = $srcinfo [1]-$waterinfo [1];      Break      Default: $x = $y = 0;      } imagecopymerge ($SRCIMGOBJ, $WATERIMGOBJ, $x, $y, 0, 0, $waterinfo [0], $waterinfo [1], $alpha);      Switch ($srcinfo [2]) {case 1:imagegif ($SRCIMGOBJ, $savefile); Case 2:imagejpeg ($SRCIMGOBJ, $savefile);      Break Case 3:imagepng ($SRCIMGOBJ, $savefile);      Break  default:return-5;      Save failed} Imagedestroy ($SRCIMGOBJ);      Imagedestroy ($WATERIMGOBJ);  return $savefile;      }//Create Picture function Image_create_from_ext ($imgfile) {$info = getimagesize ($imgfile);      $im = null;      Switch ($info [2]) {Case 1: $im =imagecreatefromgif ($imgfile); Case 2: $im =imagecreatefromjpeg ($imgfile);      Break Case 3: $im =imagecreatefromPNG ($imgfile);      Break  } return $im; }//Create Size function ImageSize ($pathimg) {//Picture equal to zoom//because PHP can only operate on resources, so copy the picture that needs to be scaled, create it as a new resource $src =IMAGECREATEFR        Omjpeg ($PATHIMG);    Get the width and height of the source image $size _src=getimagesize ($pathimg);    $w = $size _src[' 0 '];        $h = $size _src[' 1 '];        Specifies the maximum width (and possibly height) of the scale $max = 150;        Depending on the maximum value of 300, the length of the other edge is calculated, and the image width and height are scaled if ($w > $h) {$w = $max;    $h = $h * ($max/$size _src[' 0 ']);        }else{$h = $max;    $w = $w * ($max/$size _src[' 1 ']);            }//Declare a $w-wide, $h-high true Color picture resource $image =imagecreatetruecolor ($w, $h); Key functions, parameters (target resource, source, target resource start coordinate x, Y, source resource start coordinates x, Y, target resource width high w,h, source resource's width high w,h) imagecopyresampled ($image, $src, 0, 0, 0, 0, $w, $h, $        size_src[' 0 '], $size _src[' 1 ']);   Tell the browser to parse//header (' content-type:image/png ') in image format;   Imagepng ($image);   $timestamp = time ();         Imagejpeg ($image, "$timestamp. jpg"); Destruction of resources Imagedestroy ($image); return $timestamp.    JPG '; }

PHP image Processing

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.