Resizing Image Binary AFTER being in MySQL Blob?

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Feb 17, 2005
  • Posts: 1585
  • Status: Offline

Post May 27th, 2007, 4:46 pm

I have a bunch of pictures in some MySQL blobs. Normally to view them I just throw out some image headers with the image file length and then print out the binary and wam, magic picture file. How can I resize the image (IE modify the binary) before outputting to browser? I cannot resize them before putting them into the database because I need the full picture for other things so I need it resized after.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 27th, 2007, 4:46 pm

  • Truce
  • Guru
  • Guru
  • No Avatar
  • Joined: Apr 25, 2004
  • Posts: 1477
  • Loc: Washington DC
  • Status: Offline

Post May 27th, 2007, 11:04 pm

Assuming you're using PHP...

PHP Code: [ Select ]
<?
 
 
 
$desired_width = 500;
 
$desired_height = 500;
 
 
 
$im = imagecreatefromstring($blobcontents);
 
$new = imagecreatetruecolor($desired_width, $desired_height);
 
 
 
$x = imagesx($im);
 
$y = imagesy($im);
 
 
 
imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);
 
 
 
imagedestroy($im);
 
 
 
header('Content-type: image/jpeg');
 
imagejpeg($new, null, 85);
 
 
 
?>
  1. <?
  2.  
  3.  
  4.  
  5. $desired_width = 500;
  6.  
  7. $desired_height = 500;
  8.  
  9.  
  10.  
  11. $im = imagecreatefromstring($blobcontents);
  12.  
  13. $new = imagecreatetruecolor($desired_width, $desired_height);
  14.  
  15.  
  16.  
  17. $x = imagesx($im);
  18.  
  19. $y = imagesy($im);
  20.  
  21.  
  22.  
  23. imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);
  24.  
  25.  
  26.  
  27. imagedestroy($im);
  28.  
  29.  
  30.  
  31. header('Content-type: image/jpeg');
  32.  
  33. imagejpeg($new, null, 85);
  34.  
  35.  
  36.  
  37. ?>


I think you know enough to figure that code out on your own. Regardless, if you need help with it just reply.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Feb 17, 2005
  • Posts: 1585
  • Status: Offline

Post May 28th, 2007, 12:54 am

Yeah PHP.

Okay imagecreatefromstring(), thx.
  • Truce
  • Guru
  • Guru
  • No Avatar
  • Joined: Apr 25, 2004
  • Posts: 1477
  • Loc: Washington DC
  • Status: Offline

Post May 28th, 2007, 8:40 pm

Yeah, I take it you just didn't know that function.

I actually use imagecreatefromstring always since it will work regardless of the image type, so long as GD supports it. It saves me the work of doing the conditionals with imagecreatefrom(jpeg|png|gif|gd|gd2|bmp|etc). ;)
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Feb 17, 2005
  • Posts: 1585
  • Status: Offline

Post May 29th, 2007, 9:03 am

Hell yeah man. Yeah I didn't know the function. Thanks for the tip too man.
  • imelgrat
  • Novice
  • Novice
  • User avatar
  • Joined: Apr 11, 2007
  • Posts: 15
  • Loc: Argentina
  • Status: Offline

Post July 26th, 2007, 4:02 pm

If you want to resize images proportionally (without distortion no matter the width and height you choose), you could use this function.

It will crop the image to fit the chosen rectangle, avoiding (most of the times) the typical black bars that you get when applying imagecopyresample().


Cheers!
Iván
  • wolfdog
  • Born
  • Born
  • No Avatar
  • Joined: May 29, 2010
  • Posts: 1
  • Status: Offline

Post May 29th, 2010, 10:56 pm

Truce.. thanks a ton for this code. It worked perfect right out of the box.. and that never happens for me!

Yes.. I know it's a 3 year old thread.. but it's still great code. Thanks again!

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 149 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.