在没有fopen的PHP中使用imagecopyresampled()裁剪图像 [英] Cropping an image with imagecopyresampled() in PHP without fopen

查看:279
本文介绍了在没有fopen的PHP中使用imagecopyresampled()裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP和GD库裁剪和图像,似乎不能让裁剪工作。我想裁剪下面的图片中的黑色条,并将其调整为较小的尺寸(200乘112)。

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).

此处的图片

下面是我的PHP代码。

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();

?>

我收到以下错误:

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。

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!

推荐答案

您需要为目标创建一个图像资源,然后才能在 imagecopyresampled()

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

/ p>

Add this before the imagecopyresampled() line

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



更新



可能只需查看 imagecopy() 而不是 imagecopyresampled()

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

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

这篇关于在没有fopen的PHP中使用imagecopyresampled()裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆