使用 PHP 和 GD2 调整图像大小。我得到图像和黑色背景

发布于 2024-09-25 21:49:37 字数 2211 浏览 5 评论 0原文

我正在创建一个动态类来生成各种图像。

ImagesUtils.php

<?php
class ImagesUtils
{
    private $nombre_imagen = null;
    private $imagen = null;
    private $extension = null;
    private $directorio = null;

    private $width = null;
    private $height = null;
    private $tipo = null;

    private $final_width = null;
    private $final_height = null;
    private $nuevo_nombre = null;
    private $nuevo_directorio = null;

    public function __construct($imagen, $directorio = '')
    {
        $this->directorio = realpath("..".DS."data".DS."storage".DS."files".DS.$directorio);

        $this->imagen = $this->directorio.DS.$imagen;
        $this->nombre_imagen = $imagen;

        $this->extension = substr($imagen, strrpos($imagen, '.') + 1, strlen($imagen));

        $propiedades = getimagesize($this->imagen);

        $this->width = $propiedades["0"];
        $this->height = $propiedades["1"];
        $this->tipo = $propiedades["2"];
    }

    public function Resize($width = null, $height = null, $proporcion = true)
    {
        $this->final_width = $width;
        $this->final_height = $height;

        if(true == $proporcion)
            self::proporcion($width, $height);

        $imagen = imagecreatefromjpeg($this->imagen);

        $nueva_imagen = imagecreatetruecolor($this->final_width, $this->final_height);

        imagecopyresampled($nueva_imagen, $imagen, 0, 0, 0, 0, $this->final_width, $this->final_height, $this->width, $this->height);

        return imagejpeg($image, $this->nueva_imagen);
    }
}
?>

我如何调用:

$procesar_imagen = new ImagesUtils($imagen["nombre"]);
$procesar_imagen->Resize(640, 480);

宽度此代码工作正常...但如果我使用此:

$procesar_imagen->Resize(300, 300);

我生成的最终图像如下所示: http://i51.tinypic.com/htwx79.jpg

输入图像为:http://i51.tinypic.com/15n9ifc.jpg

我不知道如何解决它......我的 proporcion() 函数返回新的高度和宽度照片的长宽比...我检查过,值是正确的,返回的宽度是300(最终图像宽度是300...但计算黑色区域)。

I'm creating a dynamic class to generate various images.

ImagesUtils.php

<?php
class ImagesUtils
{
    private $nombre_imagen = null;
    private $imagen = null;
    private $extension = null;
    private $directorio = null;

    private $width = null;
    private $height = null;
    private $tipo = null;

    private $final_width = null;
    private $final_height = null;
    private $nuevo_nombre = null;
    private $nuevo_directorio = null;

    public function __construct($imagen, $directorio = '')
    {
        $this->directorio = realpath("..".DS."data".DS."storage".DS."files".DS.$directorio);

        $this->imagen = $this->directorio.DS.$imagen;
        $this->nombre_imagen = $imagen;

        $this->extension = substr($imagen, strrpos($imagen, '.') + 1, strlen($imagen));

        $propiedades = getimagesize($this->imagen);

        $this->width = $propiedades["0"];
        $this->height = $propiedades["1"];
        $this->tipo = $propiedades["2"];
    }

    public function Resize($width = null, $height = null, $proporcion = true)
    {
        $this->final_width = $width;
        $this->final_height = $height;

        if(true == $proporcion)
            self::proporcion($width, $height);

        $imagen = imagecreatefromjpeg($this->imagen);

        $nueva_imagen = imagecreatetruecolor($this->final_width, $this->final_height);

        imagecopyresampled($nueva_imagen, $imagen, 0, 0, 0, 0, $this->final_width, $this->final_height, $this->width, $this->height);

        return imagejpeg($image, $this->nueva_imagen);
    }
}
?>

And how I call:

$procesar_imagen = new ImagesUtils($imagen["nombre"]);
$procesar_imagen->Resize(640, 480);

Width this code works fine... but if I use this:

$procesar_imagen->Resize(300, 300);

My final image generated looks like: http://i51.tinypic.com/htwx79.jpg

The input image is: http://i51.tinypic.com/15n9ifc.jpg

I don't know how to solve it... my proporcion() function returns the new height and width from the aspect ratio of the photo... I checked and the values are right, the width returned is 300 (and the final image width is 300... but counting the black area).

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

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

发布评论

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

评论(1

冷清清 2024-10-02 21:49:37

我知道您正在尝试编写自己的代码,但您可能想看看这个:http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

I know you're trying to write your own code, but you might want to have a look at this: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

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