/*
- The program does not overwrite the picture's original image by limiting the width of the image to a simple function that scales the picture.
- Picture equal to zoom function parameter description:
- $IMGSRC The original image address can be a remote picture or a path on the server.
- $newimgname the name of the picture after shrinking
- $kuan Limit the width of the picture, if it is larger than the width of the image to narrow down the operation.
Example:
- $IMGSRC = "http://www.xingzuo51.com/upload/20141116/20141116010041-0.jpg";//Can be a remote picture.
- $newimgname = "Upload/20141116/000.jpg"; Do not add "/" in front of local path
- $kuan = 600;
- Img_suofang ($IMGSRC, $newimgname, $kuan); Call Picture shrink function
- */
- function Img_suofang ($IMGSRC, $newimgname, $kuan) {
- $info = getimagesize ($IMGSRC); Get picture information
- List ($w, $h) = $info;
- $BL = ($h/$w);
- Print_r ($info);
- Die ();
- $type = $info [2];
- Die ($type);
- if ($w > $kuan) {
- $k = $kuan;
- $g = ($k * $bl);
- Switch ($info [2]) {
- Case 1:
- $im = Imagecreatefromgif ($IMGSRC);
- $n = Imagecreatetruecolor ($k, $g);
- Imagecopyresampled ($n, $im, 0,0,0,0, $k, $g, $w, $h);
- $type = ". gif";
- Imagegif ($n, $newimgname. " $type ");
- Break
- Case 2:
- $im = Imagecreatefromjpeg ($IMGSRC);
- $n = Imagecreatetruecolor ($k, $g);
- Imagecopyresampled ($n, $im, 0,0,0,0, $k, $g, $w, $h);
- $type = ". jpg";
- Imagejpeg ($n, $newimgname. $type);
- Break
- Case 3:
- $im = Imagecreatefrompng ($IMGSRC);
- $n = Imagecreatetruecolor ($k, $g);
- Imagecopyresampled ($n, $im, 0,0,0,0, $k, $g, $w, $h);
- $type = ". png";
- Imagepng ($n, $newimgname. $type);
- Break
- Default
- Die ("No jpg Image");
- Break
- }
if ($im && $n) {
- echo "generated thumbnails successfully.
";
- }else{
- echo "failed to generate thumbnails. ";
- }
- Imagedestroy ($im);
- Imagedestroy ($n);
- }else{
- echo "Picture does not exceed the specified width and does not need to be scaled. ";
- }
- }
Copy Code |