Monday, May 16, 2011

Transparent area goes black after copying/cropping PNG image using PHP GD library

Recently I was working on a project in which I needed to do some image manipulations using PHP GD library. Specifically I needed to crop some areas from PNG images. I was able to crop the images pretty easily, but there was a problem in the resulting images: the transparent areas were going black which was making the resulting images unusable. To solve this problem I had to do the following:

$cropped_image = ImageCreateTrueColor($crop_width, $crop_height);
imagealphablending( $cropped_image, false );
imagesavealpha( $cropped_image, true );
$transparent = imagecolorallocatealpha($cropped_image, 0, 0, 0, 127);
imagefill($cropped_image, 0, 0, $transparent);
$original_image = ImageCreateFromPNG($image_to_crop);
imagealphablending( $original_image, false );
imagesavealpha( $original_image, true );
ImageCopy($cropped_image, $original_image, 0, 0, $left, $top, $width, $height);
ImagePNG($cropped_image, $save_path);
Here $image is the cropped image. $width and $height are the width and height of the original image respectively. $left and $top are the left and top location of the rectangular portion of the original image which should be cropped. And $save_path is the location where the cropped image should be saved.

Hope this helps someone.

Monday, January 24, 2011

Reset auto_increment value of a MySQL database table after deleting several rows

In a MySQL table with a lot of data, it is very common that last few rows will get deleted frequently. If any column has auto_increment, this will result in a gap in the generated column values. It is very easy to reset the auto_increment value to the correct value which will not result in any gap between the column values. Of course, it is only possible to prevent the gap if the deleted rows were the last few rows of the table. Here is the sql that will reset auto_increment:
ALTER TABLE table_name_to_reset AUTO_INCREMENT=1
That's it. Hope this helps.

Sunday, November 28, 2010

Time to say goodbye to FBML

It seems like it's time to say goodbye to FBML. Facebook will stop allowing to create new FBML applications soon. They will also allow iframe in profile tabs. They are encouraging iframe for both canvas applications and profile tabs. Check this link for more: http://developers.facebook.com/blog/post/415