【问题标题】:How can i fit a image into a frame without losing ratio using PHP如何使用 PHP 在不丢失比例的情况下将图像放入框架中
【发布时间】:2018-10-02 13:15:02
【问题描述】:

实际上,在这里用户可以上传任何尺寸的图像以适合我拥有的任何框架。我想在不丢失比例的情况下将该图像放入框架中。

例子:

This image is not fitting with my php code.

我的代码是

list($width, $height) = getimagesize($img1);
        $outputImage = imagecreatetruecolor($width, $height);

        // set background to white
        //$white = imagecolorallocate($outputImage, 255, 255, 255);
        //imagefill($outputImage, 0, 0, $white);
        list($width1, $height1) = getimagesize($img2);
        $info1 = getimagesize($img1);
        $info2 = getimagesize($img2);
        $extension1 = image_type_to_extension($info1[2]);
        $extension2 = image_type_to_extension($info2[2]);
        if($extension1=='.jpeg') {
        $first = imagecreatefromjpeg($img1);
        } else { 
        $first = imagecreatefrompng($img1);
        }
        if($extension2=='.jpeg') {
        $second = imagecreatefromjpeg($img2);
        } else { 
        $second = imagecreatefrompng($img2);
        }
        $ratio = $width1 / $height1;
        $targetWidth = 300 * $ratio;
        //echo $targetWidth;
        //imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
        imagecopyresampled($outputImage,$first,0,0,0,0, $width, $height, $width, $height);
        imagecopyresampled($outputImage,$second,110,75,0,0, $targetWidth, 300, $width1, $height1);

请帮助我。提前致谢。

【问题讨论】:

    标签: php image


    【解决方案1】:

    要实现这一点,您必须检测帧图像和源图像尺寸 重新调整源图像的大小以适合帧图像尺寸。 请注意,为了避免比例问题,我们将检测源图像是宽图像还是高图像(风景或肖像) 如果 width > height 则将宽度设置为框架的宽度并根据主要比率计算新高度 如果高度 > 宽度 将高度设置为框架高度并计算新宽度 这是示例实现代码

    <?
    $src  = imagecreatefromjpeg('source.jpg');//source image path
    $frame = imagecreatefrompng('frame.png');//frame image
    $sourceWidth = imagesx($src); // source image width
    $sourceHeight = imagesy($src); // image image height
    $frameWidth = imagesx($frame);//get frame width
    $frameHeight = imagesy($frame);//get frame height
    $NewImage = imagecreatetruecolor($frameWidth,$frameHeight);//the new image
    $bg_color = imagecolorallocate ($NewImage, 255, 255, 255);
    imagefill($NewImage, 0, 0, $bg_color);//set background to white
    if($sourceHeight >= $sourceWidth){
    $newHeight=$frameHeight;// set height to frame height
    $newWidth=intval($frameWidth*$sourceWidth/$sourceHeight);//calculate the new width  
    imagecopyresampled($NewImage, $src, intval(($frameWidth-$newWidth)/2),0,0,0,$newWidth, $newHeight, $sourceWidth, $sourceHeight);//insert source image to new image at the center
    } else {
    $newWidth=$frameWidth;;// set width to frame width
    $newHeight=intval($frameHeight*$sourceHeight/$sourceWidth);//calculate the new height 
    imagecopyresampled($NewImage, $src,0,intval(($frameHeight-$newHeight)/2),0,0,$newWidth, $newHeight,$sourceWidth, $sourceHeight);//insert source image to new image at the center
    }
    imagecopyresampled($NewImage, $frame, 0,0, 0,0, $frameWidth, $frameHeight, $frameWidth, $frameHeight);//copy frame imageto new image 
    header("Content-type: image/png");
    imagejpeg($NewImage);
    ?>
    

    希望这会有所帮助。

    【讨论】:

    • 是的,谢谢,这对我有帮助,但只是为了限制。实际上,当源图像很小时,框架在顶部、右侧、左侧和底部的内部都有空白。但我也想自定义框架图像以删除该空间,并且图像应该适合框架角到角。
    • 如果你想将框架的角与角匹配,你将失去源图像的比例。我们必须为源图像添加背景。为了解决这个问题,您可以使用具有不同比例的多帧图像,比如说一个用于横向,另一个用于纵向,并根据源图像的比例检测您将使用的那个。
    猜你喜欢
    • 1970-01-01
    • 2013-02-24
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2016-08-10
    • 2011-09-29
    相关资源
    最近更新 更多