PHP Picture Scaling Generate Thumbnail Function Sharing

  • 2021-06-29 10:34:16
  • OfStack


<?php
    /*
    *@im     // Picture resources that need zooming 
    *@filetype // Type of thumbnail file made 
    *@dstimW   // Width of zoomed picture 
    *@dstimH  // Height of zoomed picture 
    *@thumbname // Thumbnail file name 
function makethumb($im,$dstimW,$dstimH,$thumbname ,$filetype){
            // Obtain im Width and height 
        $pic_W=imagesx($im);
        $pic_H=imagesy($im);
        $arr = array();
            swith($filetype){
                case 'jpg':
                        $arr[$filetype]="imagejpeg";
                        break;
                case 'png';
                        $arr[$filetype]="imagepng";
                        break;
                case 'jif';
                        $arr[$filetype]="imagegif";
                }
        if(($dstimgW && $dstimgW<$pic_W) || ($dstimgH && $dstimgH<$pic_H) ){
                if($dstimgW && $dstimgW<$pic_W){
                    $dsimgWratio = $dstimgW / $pic_w;
                    $resizereagW =true;
                }
                if($dstimgH && $ $dstimgH <$pic_H){
                    $dsimgHratio = $dstimgH/$pic_H;
                    $resizerreagH =true;
                }
                // Thumbnail width-to-height ratio to original, whichever is the smallest 
                if($resizereagW && $resizerreagH){
                    if($dsimgWratio<$dsimgHratio)
                        $radio = $dsimgWratio;
                    else
                        $radio = $dsimgHratio;      
                }
                if($resizereagW && !$resizerreagH ){
                        $radio = $dsimgWratio;
                }
                if(!$resizereagW && $resizerreagH){
                       $radio = $dsimgHratio ;
                }
                $imgnewW = $pic_W * $radio;
                $imgnewH = $pic_H * $radio;
                if(function_exists("imgcopyresampled")){
                      // Create Target Resource Canvas 
                    $dst = imagecreatetruecolor ($imgnewW, $imgnewH);
                    imagecopyresampled ($dst,$im,0,0,0,0,$imgnewW,$imgnewH,$pic_W,$pic_H);
                }else{
             $dst=imagecreate($imgnewW, $imgnewH);
             imagecopyresized ($dst, $im,0,0,0,0,$imgnewW,$imgnewH,$imgnewH,$pic_W,$pic_H);
                }
        $arr[$filetype]($dst,$thumbname.".$filetype");
        imagedestroy ($dst);
    }else{// The thumbnail's own width and height are larger than the original 
           // The width and height of the thumbnail are the width and height of the original 
         $arr[$filetype]($im,$thumbname.".$filetype");
         imagedestroy();
    }
}
?>


Related articles: