在 PHP 中使用 imagecopyresampled() 裁剪图像而不使用 fopen

发布于 2024-11-28 12:57:26 字数 1976 浏览 0 评论 0原文

我正在尝试使用 PHP 和 GD 库进行裁剪和图像处理,但似乎无法进行裁剪。我想从下面的图像中裁剪出黑条并将其调整为较小的尺寸(200 x 112)。

图片位于此处

下面是我的 PHP 代码。

<?
function load_file_from_url($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $str = curl_exec($curl);
    curl_close($curl);
    return $str;
}

class cropImage{
 var $imgSrc,$myImage,$thumb;
 function setImage($image) {
       //Your Image
         $this->imgSrc = $image; 

       //create image from the jpeg
         $this->myImage = imagecreatefromstring($this->imgSrc) or die("Error: Cannot find image!"); 
     imagecopyresampled($this->thumb, $this->myImage, 0, 0, 0, 45, 200, 112, 480, 270);       
    }
    function renderImage()
    {                            
         header('Content-type: image/jpeg');
         imagejpeg($this->thumb);
         imagedestroy($this->thumb); 
         //imagejpeg($this->myImage);
         //imagedestroy($this->myImage); 
    }
}  

    $image = new cropImage;
    $image->setImage(load_file_from_url($_GET['src']));
    $image->renderImage();

?>

我收到以下错误:

PHP Warning:  imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/root/vic/boilerBytes/thumbnail.php on line 21
[Tue Aug 09 22:57:06 2011] [error] PHP Warning:  imagejpeg(): supplied argument is not a valid Image resource in /var/www/html/root/vic/boilerBytes/thumbnail.php on line 26
[Tue Aug 09 22:57:06 2011] [error] PHP Warning:  imagedestroy(): supplied argument is not a valid Image resource in /var/www/html/root/vic/boilerBytes/thumbnail.php on line 27

当我使用 $this->myImage 参数取消注释这两个方法并使用 $this->thumb 参数注释这两个方法时,原始图像正确显示,因此我认为问题出在 imagecopyresampled() 上。注意:我无法启用 fopen,所以这就是我使用curl 的原因。任何帮助将不胜感激!

I am trying to crop and image using PHP and the GD library and cannot seem to get the cropping to work. I would like to crop the black bars out of the following image and resize it to a smaller size (200 by 112).

Image located here

Below is my PHP code.

<?
function load_file_from_url($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $str = curl_exec($curl);
    curl_close($curl);
    return $str;
}

class cropImage{
 var $imgSrc,$myImage,$thumb;
 function setImage($image) {
       //Your Image
         $this->imgSrc = $image; 

       //create image from the jpeg
         $this->myImage = imagecreatefromstring($this->imgSrc) or die("Error: Cannot find image!"); 
     imagecopyresampled($this->thumb, $this->myImage, 0, 0, 0, 45, 200, 112, 480, 270);       
    }
    function renderImage()
    {                            
         header('Content-type: image/jpeg');
         imagejpeg($this->thumb);
         imagedestroy($this->thumb); 
         //imagejpeg($this->myImage);
         //imagedestroy($this->myImage); 
    }
}  

    $image = new cropImage;
    $image->setImage(load_file_from_url($_GET['src']));
    $image->renderImage();

?>

I receive the following errors:

PHP Warning:  imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/root/vic/boilerBytes/thumbnail.php on line 21
[Tue Aug 09 22:57:06 2011] [error] PHP Warning:  imagejpeg(): supplied argument is not a valid Image resource in /var/www/html/root/vic/boilerBytes/thumbnail.php on line 26
[Tue Aug 09 22:57:06 2011] [error] PHP Warning:  imagedestroy(): supplied argument is not a valid Image resource in /var/www/html/root/vic/boilerBytes/thumbnail.php on line 27

When I uncomment the two methods with $this->myImageparameters and comment the two methods with $this->thumbparameters, the original image properly displays, so I'm thinking the issue arises with imagecopyresampled(). Note: I do not have the ability to enable fopen, so this is why I'm using curl. Any help would be greatly appreciated!

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

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

发布评论

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

评论(1

极度宠爱 2024-12-05 12:57:26

您需要先为目标创建图像资源,然后才能在 imagecopyresampled() 中使用它。

imagecopyresampled() 行之前添加此内容

$this->thumb = imagecreatetruecolor(200, 112);

更新

对于裁剪,您可能应该只查看 imagecopy() 而不是 imagecopyresampled()

请随意查看我的图像处理类以获取一些想法 - https://gist.github.com/880506

You need to create an image resource for the destination before using it in imagecopyresampled().

Add this before the imagecopyresampled() line

$this->thumb = imagecreatetruecolor(200, 112);

Update

For cropping, you should probably just look at imagecopy() instead of imagecopyresampled()

Feel free to have a look at my image manipulation class for some ideas - https://gist.github.com/880506

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