PHP implementation picture scaling, super detailed annotation

Source: Internet
Author: User

The size of the processing picture that is implemented in PHP.

Comments are in the code, just look at the code.
This code also applies to viewing a processed picture on a Web page, simply by
Head Add header ("Content-type:image/png");
To remove the return value from the end of the function body,
The $outtype ($des _img, $newname) is changed into $outtype ($des _img); Can.

/** <?php//header ("Content-type:image/png"); * @todo to scale a picture according to user-defined width and rename the processed picture to the specified folder * @param string $width: User-defined target width * @param string $height: User-defined need handling The target height * @param string $filename: The name of the picture is * @param string $path: Optional parameter, the path to the new picture saved, and if no value is passed, the default current directory * @return string $newname: Returned is the new name of the processed picture/function deal ($width, $height, $filename, $path = ') {/* getimagesize get the data of the image, the return value is an array, * arr[0] Represents the width of the picture, arr[1] represents the height of the picture, arr[2] represents the image type, and returns a number * Arr[3] representing a string containing the high width of the picture, such as width= ' height= ' 100 ', which can be used directly inside the HTML tag * arr [Bits] represents the number of bits for each color of the image, using binary representations of the MIME information of the * Arr[mime] image, such as Image/png * Arr[channels]: The channel value of the image */$arr = Getima

     Gesize ($filename);
             To determine the format of the incoming picture, create and generate the corresponding picture format switch ($arr [' MIME ']) {//mime Gets the MIME value of the image, used to determine the format of creating the picture and the format of the picture generated the case "Image/png":
             $srcType = ' imagecreatefrompng ';
             $outType = ' imagepng ';
         Break
             Case "Image/gif": $srcType = ' imagecreatefromgif '; $outType= ' imagegif ';
         Break
             Case "Image/jpg": Case "image/jpeg": $srcType = ' imagecreatefromjpeg ';
             $outType = ' imagejpeg ';
     Break
    $src _img = $srcType ($filename);  $SRC _w = $arr [0];  Gets the true width of the incoming picture $src _h = $arr [1];   Gets the true height of the incoming picture $des _w = $width;  The user-defined incoming width, that is, the target width $des _h = $height;   The user-defined incoming height, that is, the target height $scale _w = $src _w/$des _w;   Gets the ratio between the true width and the target width $scale _h = $src _h/$des _h;
        Gets the ratio of the true height to the target height if ($src _w <= $des _w && $src _h <= $des _h) {$true _w = $src _w;
        $true _h = $src _h;

        $des _img = Imagecreatetruecolor ($true _w, $true _h);
        If Scale_w > Scale_h, that is, the width of the original picture is high, horizontal picture, based on a wide base}elseif ($scale _w >= $scale _h) {$true _w = $src _w/$scale _w;
        $true _h = $src _h/$scale _w;

        $des _img = Imagecreatetruecolor ($true _w, $true _h);
        Otherwise is the original picture of tall and wide, vertical picture, based on high as the benchmark to scale}else{$true _w = $src _w/$scale _h; $true _h = $src _h/$Scale_h;
    $des _img = Imagecreatetruecolor ($true _w, $true _h);

    Imagecopyresized ($des _img, $src _img, 0, 0, 0, 0, $true _w, $true _h, $src _w, $src _h);  The following is a picture of the renamed operation $temp = Explode ('/', $filename);
    Split the path, file name into an array, easy to operate, such as: Img/new/a.png, was split into temp[' img ', ' new ', ' a.png '];  $temp = Array_pop ($temp); Returns the last parameter value with the pop deletion, which is the name of the picture, such as a.png $ext = substr ($temp, Strrpos ($temp, '. ')); /get the suffix name of the picture, intercept it by string, from the last one. The location of the occurrence is intercepted to the end, such as. png $name = substr ($temp, 0,strrpos ($temp, '. '); /Get the name of the picture, intercept it by string, and intercept the last one. All characters before the position appearing, such as a $truePath =!empty ($path)? $path. " /" : ''; Save the pathname in the variable Truepath//If the path exists, and is not a path name, such as C.txt, creates a path (folder) named C.txt, executes the next sentence if the path exists and is a pathname, and executes the next sentence if the path name is not passed in (!empty
    $truePath) &&!is_dir ($truePath)) {mkdir ($truePath, 0777,true); ///give the picture a new name, with the path name + original name + custom character + suffix to name. The picture will be stored under the appropriate path $newname = $truePath. $name. "
    _deal ". $ext;

    $outType ($des _img, $newname);

    $outType ($des _img);  
 return $newname;
 } $des _w = 200;
 $des _h = 250;$filename = ' img/100-100.png ';
 $name = Deal ($des _w, $des _h, $filename); 


 Var_dump ($name);  ?>

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.