Resizing Image Binary AFTER being in MySQL Blob?

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

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
  • Posts: 1585

Post 3+ Months Ago

Yeah PHP.

Okay imagecreatefromstring(), thx.
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

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
  • Posts: 1585

Post 3+ Months Ago

Hell yeah man. Yeah I didn't know the function. Thanks for the tip too man.
  • imelgrat
  • Novice
  • Novice
  • User avatar
  • Posts: 15
  • Loc: Argentina

Post 3+ Months Ago

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
  • wolfdog
  • Posts: 1

Post 3+ Months Ago

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 120 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.