-
-
/*
- A simple function that limits the image width and scales the image proportionally. The program does not overwrite the original image.
- Image proportional scaling function parameter description:
- $imgsrc The original image address can be a remote image or a path on the server.
- $newimgname is the name of the reduced image.
- $kuan limits the width of the image. If it exceeds this width, the image will be reduced.
Example:
- $imgsrc = "http://www.xingzuo51.com/upload/20141116/20141116010041-0.jpg";//Can be a remote image.
- $newimgname = "upload/20141116/000.jpg"; //Do not add "/" in front of the local path
- $kuan = 600;
- img_suofang($imgsrc,$newimgname,$kuan); //Call the image reduction function
- */
- function img_suofang($imgsrc,$newimgname,$kuan){
- $info = getimagesize($imgsrc); //Get image 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;
- }< /p>
if ($im && $n) {
- echo "Thumbnail generated successfully.
";
- }else{
- echo "Failed to generate thumbnail. ";
- }
- imagedestroy( $im );
- imagedestroy( $n );
- }else{
- echo "The image does not exceed the specified width and does not need to be scaled. ";
- }
- }
Copy code
|