Hi again;

I've been looking at the various posts regarding image cropping and not finding any that are dirt-simple enough for me to comprehend.

The best I've been able to do is take a square .jpeg, chop it in half and get a black rectangle in return.

Here is what I need.

Refer to a .jpg file, as is. and chop off the bottom half of it. (leave it the same width), save it to a new file name or overwrite the original file...that would be okay too.

So if I start with:

<?php
$imagejpg = "temp.jpg";
$img_orig = imagecreatefromjpeg($imagejpg);

$Sx = imagesx($img_orig); //varies
$Sy = imagesy($img_orig); //would typically be 30
$Dy=15; //chop off certain number of pixels
//$Dy=($Sy/2); //...or the bottom 50%

//... here is where I'm stuck ...what goes in here?
$img_dest = imagecreatetruecolor($Sx,$Dy);

??? = $_SERVER['DOCUMNET_ROOT'] . "temp_croptest.jpg"; //save the file to the directory

//show that it worked...
echo "<img src=\"temp_croptest.jpg\">";
?>

    Okay, looks like I figured it out...

    <?php
    // Create image instances
    $src = imagecreatefromjpeg('chopme.JPG');
    $dest = imagecreatetruecolor(100, 25); //if original is 100X50
    // Copy
    //imagecopy(resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h)
    imagecopy($dest, $src, 0, 0, 0, 0, 100, 25);
    // Output
    header('Content-Type: image/jpeg');
    imagejpeg($dest);
    ?>
    

    Now I just have to figure out how to add it to the initial image generation script so that the chopped image is saved.

      $src = imagecreatefromjpeg('temp.jpg'); //load an already created image
      $width=imagesx($src); //orginal image width
      $height=imagesy($src); //original image height
      $chopheight=round($height/2); //half the height of the original image.
      $dest = imagecreatetruecolor($width, $chopheight); 
      imagecopy($dest, $src, 0, 0, 0, 0, $width, $chopheight); //copies only the top 15 pixelrows from the $src image.
      imagejpeg($dest, 'temp.jpg'); //the second parameter gives a name to the file and the function saves it to the current dir
      

      Thanks, me! I've been so helpful!

        Write a Reply...