Tuesday, February 1, 2011

Thumbnail generator

script to generate thumbnail. If image you are uploading is in proper ratio it will resize and if not then crop image. It maintain actual look of image. Below how we can integrate this.


require_once ('includes/ThumbLib.inc.php');
$res1 =move_uploaded_file ( string $filename , string $destination )
$thumb = PhpThumbFactory::create($destination/$filename);
$thumb->adaptiveResize(320,50)->save("thumbnaildestination/Thumb_".$filename);
?>
you can learn more about from http://phpthumb.gxdlabs.com/

Selecting random record from MySQL database table.

Solution 1 [SQL]
SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1;
But it is slow because mysql create a temporary table with all the result rows and assigns each one of them a random sorting index.

Solution 3 [PHP]
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `table` LIMIT $offset, 1 " );
it is best option