This article shares with you the code for equal scaling of pictures using php. Friends can integrate it into the pictureupload. Children's shoes in need can For reference.
Create a new file index.php, you need to have a picture q.jpg in the statistics directory (the name of the picture can be changed according to the source code)
The source code is as follows:
<?php $filename="q.jpg"; $per=0.3; list($width, $height)=getimagesize($filename); $n_w=$width*$per; $n_h=$height*$per; $new=imagecreatetruecolor($n_w, $n_h); $img=imagecreatefromjpeg($filename); //拷贝部分图像并调整 imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height); //图像输出新图片、另存为 imagejpeg($new, "q1.jpg"); imagedestroy($new); imagedestroy($img); ?>
After running with a browser, there will be q1.jpg in the directory at the same level as index.php. This picture is a scaled picture. The path can be changed in the source code and placed in your own project or You can also write a method
Sometimes when uploading a picture, because the picture is too large, it not only takes up space and consumes traffic, but also affects browsing (the size of the picture varies). Let’s share a method of scaling images in equal proportions without distortion. In this way, no matter how big the uploaded image is, it will be automatically compressed to the range of the size value we set. Tested and proven practical.
<?php function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) { $pic_width = imagesx($im); $pic_height = imagesy($im); if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { if($maxwidth && $pic_width>$maxwidth) { $widthratio = $maxwidth/$pic_width; $resizewidth_tag = true; } if($maxheight && $pic_height>$maxheight) { $heightratio = $maxheight/$pic_height; $resizeheight_tag = true; } if($resizewidth_tag && $resizeheight_tag) { if($widthratio<$heightratio) $ratio = $widthratio; else $ratio = $heightratio; } if($resizewidth_tag && !$resizeheight_tag) $ratio = $widthratio; if($resizeheight_tag && !$resizewidth_tag) $ratio = $heightratio; $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; if(function_exists("imagecopyresampled")) { $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数 imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数 } else { $newim = imagecreate($newwidth,$newheight); imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height); } $name = $name.$filetype; imagejpeg($newim,$name); imagedestroy($newim); } else { $name = $name.$filetype; imagejpeg($im,$name); } } //使用方法: $im=imagecreatefromjpeg("./20140416103023202.jpg");//参数是图片的存方路径 $maxwidth="600";//设置图片的最大宽度 $maxheight="400";//设置图片的最大高度 $name="123";//图片的名称,随便取吧 $filetype=".jpg";//图片类型 resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用上面的函数
The above is the detailed content of PHP method to implement image scaling code without distortion. For more information, please follow other related articles on the PHP Chinese website!