This article illustrates how PHP implements resizing pictures on the server side. Share to everyone for your reference. The specific analysis is as follows:
Resizing a picture on the server side can be a lot more beneficial than a browser's processing.
This article describes how to resize a picture on the server side of PHP.
The code consists of two parts:
①imageresizer () is used to process the image
②loadimage () inserts the image URL in a simpler format
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
<?php function Imageresizer ($url, $width, $heig HT) {header (' content-type:image/jpeg '); list ($width _orig, $height _orig) = getimagesize ($url); $ratio _orig = $width _ orig/$height _orig; if ($width/$height > $ratio _orig) {$width = $height * $ratio _orig} else {$height = $width/$ratio _orig;}//This Resa Mples the image $image _p = Imagecreatetruecolor ($width, $height); $image = Imagecreatefromjpeg ($url); Imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width, $height, $width _orig, $height _orig); Output the image imagejpeg ($image _p, NULL, 100); //works with both POST and get $method = $_server[' Request_method ']; if ($method = = ' Get ') {imageresize ($_get[' url '], $_get[' W '], $_get[' h ']), elseif ($method = = ' POST ') {imageresize ($_po st[' URL '], $_post[' W '], $_post[' h ']); }//Makes the procESS simpler function LoadImage ($url, $width, $height) {echo ' image.php?url= ', UrlEncode ($url), ' &w= ', $width, ' &h = ', $height; }?> |
Usage:
?
1 2 3 |
Above code would is in a file called image.php. Images would be displayed as this: "alt=" "/> |
I hope this article will help you with your PHP program design.