PHP Picture of equal ratio zoom and add logo Watermark Detail Code and comments

Source: Internet
Author: User
 Baidu "US-Japan"/** * than the scaling function (implemented in a saved way) * @param string $picName scaled processing picture source * @param int $maxx The maximum width of the picture after scaling * @param int $maxy the most Large Height * @param string $pre The prefix name of the picture name after scaling * @return string returns the picture name (with path), such as A.jpg=>s_a.jpg */function imagenewageupdatesize ($p Icname, $maxx =100, $maxy =100, $pre = "S_") {$imageNewageInfo = Getimagenewagesize ($picName);//Get basic information about a picture $w = $ imagenewageinfo[0];//Get width $h = $imageNewageInfo [1];//get height//Get the type of picture and create a corresponding picture resource for this switch ($imageNewageInfo [2]) {Case 1:// Gif$imagenew = Imagenewagecreatefromgif ($picName); Break;case 2://jpg$imagenew = Imagenewagecreatefromjpeg ($picName) ; Break;case 3://png$imagenew = Imagenewagecreatefrompng ($picName); Break;default:die ("wrong type of picture! ");} Calculate Scaling if (($maxx/$w) > ($maxy/$h)) {$b = $maxy/$h;} else{$b = $maxx/$w;} Calculates the scaled size $NW = Floor ($w * $b); $nh = Floor ($h * $b);//Create a new image source (target image) $nimageNew = Imagenewagecreatetruecolor ($NW, $NH); /Perform imagenewagecopyresampled ($nimageNew, $imageNew, 0,0,0,0, $NW, $nh, $w, $h);//output image (depending on the type of source image, output to the corresponding type) $ Picimagenewageinfo = Pathimagenewageinfo ($picName);//parse the source image's name and path information $newpicname= $picimageNewageInfo ["DirName"]. " /". $pre. $picimageNewageInfo [" basename "];switch ($imageNewageInfo [2]) {case 1:imagenewagegif ($nimageNew, $ Newpicname); Break;case 2:imagenewagejpeg ($nimageNew, $newpicName); Break;case 3:imagenewagepng ($nimageNew, $ Newpicname); break;} Release the picture resource Imagenewagedestroy ($imageNew); Imagenewagedestroy ($nimageNew);//Returns the result return $newpicName;}  Call Echo Imagenewageupdatesize ("./imagenewages/leyangjun.jpg", 400,400, "Ss_"); The picture you want to add/** * Add a logo image watermark to a picture (implemented in a saved way) * @param string $picName processed picture source * @param string $logo watermark Picture * @param string $ The prefix name of the picture name after pre processing * @return String returns the picture name (with path), such as A.jpg=>n_a.jpg */function Imagenewageupdatelogo ($picName, $logo, $pre = "N_") {$picNameimageNewageInfo = Getimagenewagesize ($picName);//Get basic information about a picture source $logoimagenewageinfo = Getimagenewagesize ($logo); Get basic information about logo images//var_dump ($logoimageNewageInfo);//create a corresponding picture source switch ($picNameimageNewageInfo [2]) according to the image type {Case 1:// Gif$imagenew = Imagenewagecreatefromgif ($picName); bReak;case 2://jpg$imagenew = Imagenewagecreatefromjpeg ($picName); Break;case 3://png$imagenew = Imagenewagecreatefrompng ($picName); Break;default:die ("Picture type is wrong! ");} Create a corresponding image source switch ($logoimageNewageInfo [2]) {Case 1://gif$logoimagenew = imagenewagecreatefromgif ($logo) According to the logo image type; Break;case 2://jpg$logoimagenew = Imagenewagecreatefromjpeg ($logo); Break;case 3://png$logoimagenew = Imagenewagecreatefrompng ($logo); Break;default:die ("logo image type is wrong! ");} The width imagenewagecopyresampled ($imageNew, $logoimageNew, $picNameimag of the wide-logo graph of the Gao Yuantu of the high-logo graph of the Image watermark processing source graph is performed. enewageinfo[0]-$logoimageNewageInfo [0], $picNameimageNewageInfo [1]-$logoimageNewageInfo [1],0,0,$ Logoimagenewageinfo[0], $logoimageNewageInfo [1], $logoimageNewageInfo [0], $logoimageNewageInfo [1]);// Output image (depending on the type of source image, output to the corresponding type) $picimageNewageInfo = Pathimagenewageinfo ($picName);//parse the source image for name and path information $newpicname= $ picimagenewageinfo["DirName"]. " /". $pre. $picimageNewageInfo [" basename "];switch ($picNameimageNewageInfo [2]) {Case 1:imagenewagegif ($imageNeW, $newpicName); Break;case 2:imagenewagejpeg ($imageNew, $newpicName); Break;case 3:imagenewagepng ($imageNew, $ Newpicname); break;} Release the picture resource Imagenewagedestroy ($imageNew); Imagenewagedestroy ($logoimageNew);//Returns the result return $newpicName;}  Test//echo Imagenewageupdatelogo ("./imagenewages/leyangjun.jpg", "./imagenewages/logo.png"); Corresponding to add your image, and your logo watermark picture OK
  • Related Article

    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.