1

I have never used the PHP FILES array and started using it for this particular task. I am having trouble on this piece of code when retrieving the blob element from the database and then rendering out after resizing it. can you please help me out? Do I need to resize the element before uploading it or it can be done this way as well.

This is how I did it.

    <img src='data:image/jpeg;base64,".base64_encode($row['x'])." width='100'                   height='100''/>
cimmanon
  • 67,211
  • 17
  • 165
  • 171
urhydy
  • 27
  • 1
  • 8
  • 1
    If you set the width and height on the img tag, then it will only be resized client-side. You'll still be sending the full-size image to the browser, it'll just be displayed at the specified size. – crush Feb 11 '14 at 17:42
  • @crush any suggestion how I can do this I tried other ways as well didn't understand some. Would appreciate your advice – urhydy Feb 11 '14 at 17:51
  • @urhydy Try rephrasing your question to show your attempts at resizing the image. – cimmanon Feb 11 '14 at 17:59
  • If you are using the php FILES array then I assume these images are uploaded. If that is the case, do not store them in the database. Store them on disk and store the path in the database. – user20232359723568423357842364 Feb 11 '14 at 21:58

1 Answers1

0

Create the image in PHP with imagecreatefromstring

$image = imagecreatefromstring(base64_decode($row['x']));

Next, use imagescale to resize the image.

$image = imagescale($image, 100, 100);

Set the appropriate header:

header('Content-Type: image/jpeg');

Output the image with imagejpeg:

imagejpeg($image);

Free up memory by destroying the image with imagedestroy:

imagedestroy($image);

Now point your <img /> to this PHP file.

Alternatively, if putting that logic in its own file doesn't work for you, you can use Output Buffering to capture the output of the image as seen in this StackOverflow answer, and then place it directly in the src attribute.

It would go something like this:

$image = imagecreatefromstring(base64_decode($row['x']));
$image = imagescale($image, 100, 100);

ob_start();
imagejpeg($image);
$contents = ob_get_contents();
ob_end_clean();

echo "<img src='data:image/jpeg;base64,".base64_encode($contents)."' />";

imagedestroy($image);
Community
  • 1
  • 1
crush
  • 16,713
  • 9
  • 59
  • 100
  • Hello there thank you for advice but the imagescale() method isn't recognised somewhat although I see that it exist. It gives me an errorFatal error: Call to undefined function imagescale() in – urhydy Feb 11 '14 at 18:43
  • You could try using [`imagecopyresized`](http://us2.php.net/manual/en/function.imagecopyresized.php) instead – crush Feb 11 '14 at 18:46
  • Hi there now, it is recgonised but however.........i took out the imagecreatefromstring as it gave me another error which I thought because base64_decode() does almost the same thing doesn't it..... however its still giving me a different error......imagecopyresized() expects exactly 10 parameters, 3 given in file C:\xampp\htdocs\P3............. – urhydy Feb 11 '14 at 18:52
  • @urhydy Read the parameters listed on the PHP documentation page that I linked. – crush Feb 11 '14 at 18:52