-1

I am trying to crop off 45px from the top and bottom of YouTube jpeg thumbnail images, for example this one which is 480px x 360px.

It looks like this:

enter image description here

Notice the 45px black bars on the top and bottom of the image. I simply want those removed such that my resulting image is 480px x 270px and the black bars are gone.

I have achieved partial success by implementing the example from this stack post. Here is my PHP function based on that:

function CropImage($sourceImagePath, $width, $height){
    $src = imagecreatefromjpeg($sourceImagePath);
    $dest = imagecreatetruecolor($width, $height);
    imagecopy($dest, $src, 0, 0, 20, 13, $width, $height);
    header('Content-Type: image/jpeg');
    imagejpeg($dest);
    imagedestroy($dest);
    imagedestroy($src);
}

And called thusly:

CropImage("LOTR.jpg", 480, 270);

Some cropping occurs, but 2 problems result:

  1. It doesn't crop the top and bottom, rather it seems to crop the left side and the bottom, resulting in this:

enter image description here

  1. From the PHP code snippet I'm using I can't see how to generate a new file. Rather, the PHP script I execute in the browser simply renders the morphed file in the browser. I don't want this to happen, I want to be able to pass a dest path into the function and have it create the new file (and not send anything to the client/browser) with the top 45px and bottom 45px removed. It's obvious that header('Content-Type: image/jpeg'); is part of the problem, but removing that still doesn't give me a destination file written to the server, methinks.

I'm also looking in the PHP docs here. It seems like changing the params in imagecopy($dest, $src, 0, 0, 20, 13, $width, $height); would solve this, but it's really unclear to me what those params should be. The resulting thumbnails inside the YouTube tab look odd with the black bars. Thanks in advance for any advice.

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158

1 Answers1

1
<?php
function CropImage($sourceImagePath, $width, $height){

    // Figure out the size of the source image
    $imageSize = getimagesize($sourceImagePath);
    $imageWidth = $imageSize[0];
    $imageHeight = $imageSize[1];

    // If the source image is already smaller than the crop request, return (do nothing)
    if ($imageWidth < $width || $imageHeight < $height) return;

    // Get the adjustment by dividing the difference by two
    $adjustedWidth = ($imageWidth - $width) / 2;
    $adjustedHeight = ($imageHeight - $height) / 2;

    $src = imagecreatefromjpeg($sourceImagePath);

    // Create the new image
    $dest = imagecreatetruecolor($width,$height);      

    // Copy, using the adjustment to crop the source image
    imagecopy($dest, $src, 0, 0, $adjustedWidth, $adjustedHeight, $width, $height);

    imagejpeg($dest,'somefile.jpg');
    imagedestroy($dest);
    imagedestroy($src);
}
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • user2182349 your solution works perfectly except I still don't see how to save the modified image to the server. `header('Content-Type: image/jpeg');` certainly needs to be removed, but what is the syntax for saving the changed image? – HerrimanCoder Aug 27 '17 at 17:31
  • Yes, that's it. Please update your answer with that, and `header` removed. So I can accept, and it may help others. – HerrimanCoder Aug 28 '17 at 12:11