缩放图像无法正常工作?

发布于 2024-10-14 22:44:45 字数 1460 浏览 2 评论 0原文

我有一个 $this->_tempFile 保存上传图像文件的临时路径。

现在我想缩放图像,它确实调整了图像大小,但缩放比例不正确。输出是一个大的黑色正方形,底部位于图像的高度和一半(宽度)。我尝试了其他图像,结果都是这些尺寸: 293px × 453px,为什么我不知道?

这是我的 scaleImage(); 函数,

scaleImage(900, 582);

public function scaleImage($width, $height){
    $rel_difference = array('width'=>0, 'height'=>0);
    if($width > 604) $rel_difference['width'] = ($width-604)/604;
    if($height > 453) $rel_difference['height'] = ($height-453)/453;

    asort($rel_difference);
    $tmpname = $this->_tempFile;
    $newwidth = $width/(1+end($rel_difference));
    $newheight = $height/(1+end($rel_difference)); 
    $newwidth = round($newwidth);
    $newheight = round($newheight);
    $jpeg_quality = 90;

    switch(exif_imagetype($tmpname)) {
        case IMAGETYPE_GIF:
            $img_r = imagecreatefromgif($tmpname);
            break;
        case IMAGETYPE_JPEG:
            $img_r = imagecreatefromjpeg($tmpname);
            break;
        case IMAGETYPE_PNG:
            $img_r = imagecreatefrompng($tmpname);
            break;
        default:
            echo json_encode(array('error' => 'Not an image!'));
            exit(0);
            break;
    }
    $dst_r = ImageCreateTrueColor( $newwidth, $newheight );

    imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);

    imagejpeg($dst_r,$tmpname,$jpeg_quality);

    }

这里出了什么问题?

I have a $this->_tempFile that holds the temp path of an uploaded image file.

Now I wish to scale an image, it do resizes it but it doesn't scale right. The output is a big black square the bottom at the height and half of the image (width). And i tried with other images, and all turns out being these dimensions:
293px × 453px , why i don't know?

Here's my function for the scaleImage();

scaleImage(900, 582);

public function scaleImage($width, $height){
    $rel_difference = array('width'=>0, 'height'=>0);
    if($width > 604) $rel_difference['width'] = ($width-604)/604;
    if($height > 453) $rel_difference['height'] = ($height-453)/453;

    asort($rel_difference);
    $tmpname = $this->_tempFile;
    $newwidth = $width/(1+end($rel_difference));
    $newheight = $height/(1+end($rel_difference)); 
    $newwidth = round($newwidth);
    $newheight = round($newheight);
    $jpeg_quality = 90;

    switch(exif_imagetype($tmpname)) {
        case IMAGETYPE_GIF:
            $img_r = imagecreatefromgif($tmpname);
            break;
        case IMAGETYPE_JPEG:
            $img_r = imagecreatefromjpeg($tmpname);
            break;
        case IMAGETYPE_PNG:
            $img_r = imagecreatefrompng($tmpname);
            break;
        default:
            echo json_encode(array('error' => 'Not an image!'));
            exit(0);
            break;
    }
    $dst_r = ImageCreateTrueColor( $newwidth, $newheight );

    imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);

    imagejpeg($dst_r,$tmpname,$jpeg_quality);

    }

What is wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-10-21 22:44:45

我怀疑您不小心翻转了您传递的参数中的高度和宽度。也如此

scaleImage(582, 900);

I suspect you have accidentally flipped the height and width in the parameters you're passing. So do

scaleImage(582, 900);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文