开发者

How to crop and image before adding an overlay? (GD) (PHP)

My code so far (it creates an overlay to a youtube thumbnail):

<?php
header("Content-type:image/png");
$background=imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert=imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert开发者_开发知识库,0,0));
$insert_x=imagesx($insert);
$insert_y=imagesy($insert);
imagecopymerge($background,$insert,5,57,0,0,$insert_x,$insert_y,100);
imagepng($background,NULL);

The output image is 120x90px, but i need it to have 90x90px.

Anyone knows how this is possible?

Thanks in advance!


http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/

<?php

header("Content-type:image/png");

$background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");

$insert = imagecreatefrompng("play.png");

imagecolortransparent($insert,imagecolorat($insert,0,0));

list($current_width, $current_height) = getimagesize($background);

$left = 0;
$top = 0;

$crop_width = 90;
$crop_height = 90;

$canvas = imagecreatetruecolor($crop_width, $crop_height);

$current_image = $background;

imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);

imagecopymerge($canvas,$insert,5,57,0,0,$current_width,$current_height,100);

imagepng($canvas);

?>

try that it should work if not comment as to otherwise.


This is taken from a function I wrote to create square thumbnails. You may find the commentary I wrote for myself helpful. Depending on your needs (i.e if you can't afford to make assumptions about the type and dimensions of incoming images) you may need to add additional checks. To avoid smashed or stretched thumbnails we take a central part of the image which is most likely to contain something distinguishable as source co-ordinates.

The basic idea is: Since imagecopyresampled or imagecopyresized also allow you to specify destination and source coordinates, you can copy only part of your original image to a new canvas and save that (or directly output to browser). To avoid smashed or stretched dimensions we take a central part of the original image which is most likely to contain distinguishable content.

//assuming you have already merged your $background image with your overlay at this point

//original dimensions
$original_width = imagesx($background);
$original_height = imagesy($background);

//new dimensions
$new_width = 90;
$new_height = 90;

//the center of the rectangular image
$center = $original_width/2, $original_height/2

//the coordinates from where to start on the original image 
//assuming you want to crop the center part
$src_x = floor(($original_width/2) - ($new_width/2)); 
$src_y = floor(($original_height/2) - ($new_height/2)); 

//create a new canvas with the new desired width, height
$temp = imagecreatetruecolor(90,90);

//copy the large image to this new canvas
imagecopyresampled($temp,$background,0,0,$src_x,$src_y,$new_width,$new_height,$original_width,$original_height);
//from right to left: source image, destination image, dest x, dest y,
//source x, source y, new width, new height, original width, original height

//save the canvas as an image
imagepng($temp);

This could be improved to handle larger images by first taking a central part relative to it's size and then scaling it down to the new canvas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜