0

I have the following code on my php file which is working fine, Its decoding a base64 string and showing that as a image on the webpage, but i want it to save it on a folder too.

<?php
$base = $_POST['encoded'];
$base = base64_decode($base);

$im = imagecreatefromstring($base);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}
?>

i have already tried the solution described on these links but none of these are worked for me

How to save a PNG image server-side, from a base64 data string

How to create GD Image from base64 encoded jpeg?

what will be the extra line of code which could save this image to a folder

Community
  • 1
  • 1
Harish Kumar
  • 927
  • 3
  • 20
  • 46
  • `imagepng();` has a second parameter which is file name/file path. – Rasclatt Dec 11 '14 at 06:49
  • already tested with imagepng($im,'image.png'); its stop displaying image on webspage and not even save it on the folder – Harish Kumar Dec 11 '14 at 06:52
  • It would stop displaying, true but it should output to the same directory as the executing file. Is the file not in the same space as the page running this script? – Rasclatt Dec 11 '14 at 06:53
  • yeah the file is in the same place. but not saving the image on same directory – Harish Kumar Dec 11 '14 at 06:55
  • oh now i got it. its the file permission issue. writing permission was off thats why its not creating the image. now imagepng($im,'image.png'); is working – Harish Kumar Dec 11 '14 at 06:57

3 Answers3

0
$base = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       .'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       .'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       .'8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$base = base64_decode($base);

$im = imagecreatefromstring($base);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}

Well the code worked for me.
If you see a blank page, that means the string encoded was incorrect.

AkiEru
  • 780
  • 1
  • 9
  • 34
0

The file permissions was not set to writing mode. set it to 777 and tested with the following code.

<?php
$base = $_POST['encoded'];
$base = base64_decode($base);

$im = imagecreatefromstring($base);
if ($im !== false) {
    header('Content-Type: image/png');

    imagepng($im,'image.png'); // saving image to the same directory

    imagedestroy($im);

}
else {
    echo 'An error occurred.';
}
?>
Harish Kumar
  • 927
  • 3
  • 20
  • 46
0

This code works for JPEG image

$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHB'; // image data 

define('UPLOAD_DIR','dir_path/'); // image dir path

list($type, $data) = explode(';', $data); 

list(, $data)      = explode(',', $data);

$data = str_replace(' ', '+', $data);

$data = base64_decode($data); // base 64 decoding 

file_put_contents(UPLOAD_DIR.$img_name.".jpeg", $data); // saving the image to required path