<?php $base64 = "iVBORw0KGgoAAAANSUhEUgAAABw" . "AAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWN" . "gPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL5AwPnZGYGIGHMwGA" . "5mdkASNgbMNgJ80AIAMCSHqNvm2VtAAAAAElFTkSuQmCC"; function get_img_type($data) { $n = array(1,1,2,3); $e = array("474946383761","474946383961","ffd8ff","89504e470d0a1a0a"); for ($i=0; $i<4; $i++) { if (strtolower(bin2hex(substr($data,0,strlen($e[$i])/2)))==$e[$i]) return $n[$i]; } return NULL; } $data = base64_decode($base64); $tp = get_img_type($data); if ($tp) { $img = imagecreatefromstring($data); if ($img !== false) { $type = array("gif","jpeg","png"); header("Content-Type: image/" . $type[$tp-1]); if ($tp==1) imagegif($img); if ($tp==2) imagejpeg($img); if ($tp==3) imagepng($img); imagedestroy($img); } else { echo "An error occurred."; } } else { echo "Invalid format! Only GIF, JPEG and PNG."; } ?>
PHP: Create Image to base64 string (Ottiene immagine da stringa base64 corrispondente)
<?php $base64 = "iVBORw0KGgoAAAANSUhEUgAAABw" . "AAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAAP0lEQVQImWN" . "gPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL5AwPnZGYGIGHMwGA" . "5mdkASNgbMNgJ80AIAMCSHqNvm2VtAAAAAElFTkSuQmCC"; function get_img_type($data) { $n = array(1,1,2,3); $e = array("474946383761","474946383961","ffd8ff","89504e470d0a1a0a"); for ($i=0; $i<4; $i++) { if (strtolower(bin2hex(substr($data,0,strlen($e[$i])/2)))==$e[$i]) return $n[$i]; } return NULL; } $data = base64_decode($base64); $tp = get_img_type($data); if ($tp) { $img = imagecreatefromstring($data); if ($img !== false) { $type = array("gif","jpeg","png"); header("Content-Type: image/" . $type[$tp-1]); if ($tp==1) imagegif($img); if ($tp==2) imagejpeg($img); if ($tp==3) imagepng($img); imagedestroy($img); } else { echo "An error occurred."; } } else { echo "Invalid format! Only GIF, JPEG and PNG."; } ?>