How to resize a picture on the server side of PHP _php tips

Source: Internet
Author: User

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

<?php function Imageresizer ($url, $width, $height) {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 resamples 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 ($_post[' 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:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.