PHP get remote image URL to generate thumbnail image method

Source: Internet
Author: User
Tags imagejpeg

getimg.php
<?PHP/** * * function: Adjust picture size or generate thumbnails * return: True/false * Parameters: * $Image need to adjust the picture (including the path) * $Dw The maximum width of =450 adjustment, the absolute width when the thumbnail is $Dh =450 the maximum height when adjusting; The absolute height of the sketch * $Type = 1 1, adjust the size; 2, generate thumbnail image*/ $phtypes=Array(' Img/gif ', ' img/jpg ', ' img/jpeg ', ' img/bmp ', ' img/pjpeg ', ' img/x-png '); functionCompressimg ($Image,$Dw,$Dh,$Type){      IF(!file_exists($Image)){          return false; }      //If you need to generate thumbnails, copy the original image to $image assignment (generate thumbnail action)///When the type==1 is not copied, it will regenerate the reduced image on the original image file (resize operation)    IF($Type!=1){          Copy($Image,Str_replace(".", "_x.",$Image)); $Image=Str_replace(".", "_x.",$Image); }      //get the type of file, build different objects according to different types    $ImgInfo=getimagesize($Image); Switch($ImgInfo[2]){           Case1:$Img[Email protected] ($Image);  Break;  Case2:$Img[Email protected] ($Image);  Break;  Case3:$Img[Email protected] ($Image);  Break; }      //if the object is not created successfully, the non-picture file    IF(Empty($Img)){          //If an error occurs when generating thumbnails, you need to delete the files that have already been copied        IF($Type!=1){              unlink($Image); }          return false; }      //If you are performing a resize operation    IF($Type==1){          $w=imagesx ($Img); $h=imagesy ($Img); $width=$w; $height=$h; IF($width>$Dw){              $Par=$Dw/$width; $width=$Dw; $height=$height*$Par; IF($height>$Dh){                  $Par=$Dh/$height; $height=$Dh; $width=$width*$Par; }          } ElseIF($height>$Dh) {              $Par=$Dh/$height; $height=$Dh; $width=$width*$Par; IF($width>$Dw){                  $Par=$Dw/$width; $width=$Dw; $height=$height*$Par; }          } Else {              $width=$width; $height=$height; }          $nImg=imagecreatetruecolor ($width,$height);//Create a new true Color canvasImagecopyresampled ($nImg,$Img, 0,0,0,0,$width,$height,$w,$h);//Resample and resize a portion of an imageImagejpeg ($nImg,$Image);//output images to a browser or file in JPEG format        return true; } Else{//If you are performing a build thumbnail operation        $w=imagesx ($Img); $h=imagesy ($Img); $width=$w; $height=$h; $nImg=imagecreatetruecolor ($Dw,$Dh); IF($h/$w>$Dh/$Dw){//high and relatively large            $width=$Dw; $height=$h*$Dw/$w; $IntNH=$height-$Dh; Imagecopyresampled ($nImg,$Img, 0,-$IntNH/1.8, 0, 0,$Dw,$height,$w,$h); } Else{//wide Larger            $height=$Dh; $width=$w*$Dh/$h; $IntNW=$width-$Dw; Imagecopyresampled ($nImg,$Img,-$IntNW/1.8,0,0,0,$width,$Dh,$w,$h); } imagejpeg ($nImg,$Image); return true;    }  }; /** * Get pictures on server based on URL * $url Server slice path $filename file name*/  functionGrabimage ($url,$filename="") {      if($url=="")return false; if($filename=="") {          $ext=STRRCHR($url,"."); if($ext! = ". gif" &&$ext! = ". jpg" &&$ext! = ". png")              return false; $filename=Date("Ymdhis").$ext; }      Ob_start(); ReadFile($url); $img=ob_get_contents(); Ob_end_clean(); $size=strlen($img); $fp 2[Email protected]fopen($filename, "a"); fwrite($fp 2,$img); fclose($fp 2); return $filename; }  ?>

Call:

<?PHPrequire_once(' getimg.php ' );$imgPath= ' http://xxx.com/aa.jpg ';//Remote URL address$tempPath= ' adsdf.jpg ';//Save Picture Path  if(Is_file($tempPath)){      unlink($tempPath); }    $bigImg=grabimage ($imgPath,$tempPath); Var_dump(Compressimg ($bigImg, 100,100,1)); ?>

PHP get remote image URL to generate thumbnail image method

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.