php图像大小调整旋转一些图像

时间:2017-01-21 12:45:09

标签: php image resize

我使用下面的代码在PHP中调整图像大小。对于某些图像,保存的图像旋转90度。

我认为它与宽度小于其高度的图像有关。我如何修改代码以防止这种旋转?



function thumbnail( $img, $source, $dest, $maxw, $maxh ) {  
   
    $jpg = $source.$img;

    if( $jpg ) {
        list( $width, $height  ) = getimagesize( $jpg ); //$type will return the type of the image
        $source = imagecreatefromjpeg( $jpg );

        if( $maxw >= $width && $maxh >= $height ) {
            $ratio = 1;
        }elseif( $width > $height ) {
           
           $ratio = $maxw / $width;
        }else {
            
            $ratio = $maxh / $height; 
        }

        $thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
        $thumb_height = round( $height * $ratio );

        $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
        imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

        $path = $dest.$img;
        imagejpeg( $thumb, $path, 75 );
    }
    imagedestroy( $thumb );
    imagedestroy( $source );
}




0 个答案:

没有答案