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:
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:
- It doesn't crop the top and bottom, rather it seems to crop the left side and the bottom, resulting in this:
- 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.