1

我正在拍摄源图像并尝试根据用户选择对其进行裁剪。虽然它总是留下一个黑色的空白空间,但我假设它与我以错误顺序使用的参数有关。在查看了 imagecopyresampled() 的 php 文档之后,我仍然没有找到一种方法来完成这项工作。

这就是我所拥有的。我在示例中使用了硬编码值,但每个$targ_变量都会针对每个图像进行更改。

$targ_w = 183;      // width of cropped selection
$targ_h = 140;      // height of cropped selection
$targ_x = 79;           // x-coordinate of cropped selection
$targ_y = 59;           // y-coordinate of cropped selection
$targ_x2 = 263;         // x-coordinate of 2nd point (end of selection)
$targ_y2 = 199;     // y-coordinate of 2nd point (end of selection)

$src = '/uploads/test.png';
$img_r = imagecreatefrompng($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

imagecopyresampled($dst_r,$img_r,0,0,$targ_x,$targ_y,$targ_x2,$targ_y2,$targ_w,$targ_h);

$newFileName = '/uploads/test2.png';
imagepng($dst_r, $newFileName, 9);
4

2 回答 2

3

也许只是因为您没有正确计算裁剪区域的尺寸。除此之外,您使用坐标,应该使用尺寸。比较参数七和八。请注意,这不会保留透明度。

<?php
$targ_x1 = 59;      // x-coordinate of cropped selection
$targ_y1 = 69;      // y-coordinate of cropped selection
$targ_x2 = 160;     // x-coordinate of 2nd point (end of selection)
$targ_y2 = 82;      // y-coordinate of 2nd point (end of selection)

//1st: dynamically calculate the dimensions
$crop_area_width = $targ_x2-$targ_x1;      // width of cropped selection
$crop_area_height = $targ_y2-$targ_y1;      // height of cropped selection

$image_path = 'http://cdn.sstatic.net/stackoverflow/img/sprites.png';
$src_img = imagecreatefrompng($image_path);
$dst_img = imagecreatetruecolor($crop_area_width, $crop_area_height);

//2nd: cropping does not change scale so source and target dimensions are the same
imagecopyresampled( $dst_img, $src_img, 0, 0, $targ_x1, $targ_y1, $crop_area_width, $crop_area_height, $crop_area_width, $crop_area_height);

header('Content-Type: image/png');
imagepng($dst_img);
?>
于 2012-08-28T21:52:53.483 回答
0

见以下网址:-

在没有 fopen 的 PHP 中使用 imagecopyresampled() 裁剪图像

尝试这个:-

<?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!");
         $this->thumb = imagecreatetruecolor(200, 112);

        //imagecopyresampled($this->thumb, $this->myImage, 0, 0, 0, 45, 200, 112, 480, 270);
         imagecopy($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();

?>
于 2012-08-28T20:36:54.177 回答