Home >Backend Development >PHP Tutorial >PHP upload image to generate equal-proportion thumbnail code_PHP tutorial
A simple method of uploading image files using PHP and then generating a thumbnail effect of equal proportions. Friends who need to learn can refer to it.
The code is as follows | Copy code |
function _UPLOADPIC($upfile, $maxsize, $updir, $newname = 'date') { if ($newname == 'date') $newname = date ( "Ymdhis" ); //Use date as file name $name = $upfile ["name"]; $type = $upfile ["type"]; $size = $upfile ["size"]; $tmp_name = $upfile ["tmp_name"]; switch ($type) { case 'image/pjpeg' : case 'image/jpeg' : $extend = ".jpg"; break; case 'image/gif' : $extend = ".gif"; break; case 'image/png' : $extend = ".png"; break; } if (emptyempty ( $extend )) { echo ( "Warning! Only image types can be uploaded: GIF JPG PNG" ); exit (); } if ($size > $maxsize) { $maxpr = $maxsize / 1000; echo ( "Warning! The uploaded image size cannot exceed" . $maxpr . "K!" ); exit (); } if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) { return $updir . $newname . $extend; } }
function show_pic_scal($width, $height, $picpath) { $imginfo = GetImageSize ( $picpath ); $imgw = $imginfo [0]; $imgh = $imginfo [1];
$ra = number_format ( ($imgw / $imgh), 1 ); //Aspect ratio $ra2 = number_format ( ($imgh / $imgw), 1 ); //Aspect Ratio
if ($imgw > $width or $imgh > $height) { if ($imgw > $imgh) { $newWidth = $width; $newHeight = round ( $newWidth / $ra );
} elseif ($imgw < $imgh) { $newHeight = $height; $newWidth = round ( $newHeight / $ra2 ); } else { $newWidth = $width; $newHeight = round ( $newWidth / $ra ); } } else { $newHeight = $imgh; $newWidth = $imgw; } $newsize [0] = $newWidth; $newsize [1] = $newHeight;
return $newsize; }
function getImageInfo($src) { return getimagesize($src); } /** * Create image and return resource type * @param string $src image path * @return resource $im Return resource type * **/ function create($src) { $info=getImageInfo($src); switch ($info[2]) { case 1: $im=imagecreatefromgif($src); break; case 2: $im=imagecreatefromjpeg($src); break; case 3: $im=imagecreatefrompng($src); break; } return $im; } /** * Thumbnail main function * @param string $src image path * @param int $w thumbnail width * @param int $h Thumbnail height * @return mixed Return to thumbnail path * **/
function resize($src,$w,$h) { $temp=pathinfo($src); $name=$temp["basename"];//File name $dir=$temp["dirname"];//The folder where the file is located $extension=$temp["extension"];//File extension $savepath="{$dir}/{$name}";//Thumbnail save path, the new file name is *.thumb.jpg
//Get basic information about the image $info=getImageInfo($src); $width=$info[0];//Get the image width $height=$info[1];//Get the image height $per1=round($width/$height,2);//Calculate the aspect ratio of the original image $per2=round($w/$h,2);//Calculate the thumbnail aspect ratio
//Calculate scaling ratio if($per1>$per2||$per1==$per2) { //The aspect ratio of the original image is greater than or equal to the aspect ratio of the thumbnail, then the width will take precedence $per=$w/$width; } if($per1<$per2) { //The aspect ratio of the original image is smaller than the aspect ratio of the thumbnail, then priority will be given by height $per=$h/$height; } $temp_w=intval($width*$per);//Calculate the width of the original image after scaling $temp_h=intval($height*$per);//Calculate the height of the original image after scaling $temp_img=imagecreatetruecolor($temp_w,$temp_h);//Create canvas $im=create($src); imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height); if($per1>$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return addBg($savepath,$w,$h,"w"); //Width priority, fill in the background if the height is insufficient after scaling } if($per1==$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return $savepath; //Constant ratio scaling } if($per1<$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return addBg($savepath,$w,$h,"h"); //Height priority, add the background if the width is insufficient after scaling } } /** * Add background * @param string $src image path * @param int $w background image width * @param int $h Background image height * @param String $first determines the final position of the image, w width first h height priority wh: equal ratio * @return Return the picture with background * **/ function addBg($src,$w,$h,$fisrt="w") { $bg=imagecreatetruecolor($w,$h); $white = imagecolorallocate($bg,255,255,255); imagefill($bg,0,0,$white);//Fill the background
//Get target image information $info=getImageInfo($src); $width=$info[0];//Target image width $height=$info[1];//Target image height $img=create($src); if($fisrt=="wh") { //Constant ratio scaling return $src; } else { if($fisrt=="w") { $x=0; $y=($h-$height)/2;//Vertically centered } if($fisrt=="h") { $x=($w-$width)/2;//Horizontally centered $y=0; } imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100); imagejpeg($bg,$src,100); imagedestroy($bg); imagedestroy($img); return $src; } How to use: $filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date')); |