PHP Custom Picture scaling function to achieve equal scale without distortion scaling method _php Tips

Source: Internet
Author: User
Tags imagejpeg php database

This article illustrates a method for scaling the scale without distortion, such as the PHP custom picture scaling function. Share to everyone for your reference, specific as follows:

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 = $wid
      Thratio;
    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);
    Imagecopyresampled ($newim, $im, 0,0,0,0, $newwidth, $newheight, $pic _width, $pic _height);
      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);

 }
}

Parameter description:

$im a Picture object, you need to read the image object with Imagecreatefromjpeg () before applying the function, and if the PHP environment supports Png,gif, you can also use Imagecreatefromgif (), imagecreatefrompng ();

$maxwidth define the maximum width (in pixels) of the resulting picture

$maxheight The maximum height of a picture to be generated (in pixels)

$name the generated picture name

$filetype the resulting picture type (. jpg/.png/.gif)

Code comments:

Line 3rd to 4th: Read the picture that needs to be scaled. Actual width height

Line 8th to 26th: by calculating the actual picture width and the need to generate a picture of the width of the high compression ratio of the final picture scaling is based on the width or height of the zoom, the current program is based on the width of the picture scaling. If you want to scale the picture according to the height, you can change the statement in line 22nd to $widthratio> $heightratio

Line 28th to 31st: If the actual picture's length or width is less than the length or width of the resulting picture, either the picture is scaled according to the length, or the picture is scaled according to its width.

Line 33rd to 34th: Calculates the long width of the picture generated by the final scaling.

Line 36th to 45th: According to the calculation of the final generated picture of the length of the image to change the size of the picture, there are two ways to change the size of the Picture: imagecopyresized () function in all GD version of the effective, but its scaling image algorithm is relatively rough. Imagecopyresamples (), its pixel interpolation algorithm gets the edge of image smoother, but the function is slower than imagecopyresized ().

Line 47th to 49th: The resulting processed picture, if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng ()

Line 51st to 56th: if the actual picture's length is smaller than the length of the specified picture, keep the picture intact, and similarly, if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng ().

Special Note:

The 1.6.2 version of the GD library previously supported the GIF format, but the GIF format was not supported after the GD1.6.2 version because the LZW algorithm involved patent rights. If you are Windows environment, you just go into the php.ini file to find Extension=php_gd2.dll, will # Remove, restart Apache, if you are a Linux environment, and want to support gif,png,jpeg, you need to download libpng , zlib, and FreeType fonts and install.

For more information on PHP related content readers can view the site topics: "PHP graphics and picture Operation skills Summary", "PHP array of Operations Skills encyclopedia", "PHP Mathematical Calculation Skills Summary", "PHP date and Time usage summary", "PHP object-oriented Programming Program", Summary of PHP string usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the 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.