简体   繁体   中英

imagecopyresampled cropping black bar

I am taking a source image and trying to crop it based on user selection. It always leaves a black empty space though, I am assuming it has something to do with the parameters I'm using in the incorrect order. After looking at php docs for imagecopyresampled() I am still coming up short on a way to make this work.

Here is what I have. I used hardcoded values for the example but each $targ_ variable changes for each image.

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

Maybe it is just because you did not calculate the crop area's dimensions correctly. Besides that you use coordinates, where dimensions should have been used. Compare parameter seven and eight. Note that this does not preserve transparency.

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

See below URL:-

Cropping an image with imagecopyresampled() in PHP without fopen

try this:-

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM