I have the following function that should convert a image form an url to a base64encoded inline-Image.
function imageToBase64Jpg($imagePath, $maxWidth=825){
$imageType = exif_imagetype($imagePath);
if ($imageType == IMAGETYPE_WEBP) $image = imagecreatefromwebp($imagePath);
elseif ($imageType == IMAGETYPE_JPEG) $image = imagecreatefromjpeg($imagePath);
else return '<img src="'.$imagePath.'">';
if ($image === false) return '<img src="'.$imagePath.'">';
$originalWidth = imagesx($image);
if ($originalWidth > $maxWidth) {
$originalHeight = imagesy($image);
$newWidth = $maxWidth;
$newHeight = (int) (($newWidth / $originalWidth) * $originalHeight);
$image = imagescale($image, $newWidth, $newHeight);
}
ob_start();
imagejpeg($image, null, 80);
$jpgImage = ob_get_contents();
ob_end_clean();
imagedestroy($image);
// imagejpeg($image,'test.jpg',80);
return '<img src="data:image/jpeg;base64,' . base64_encode($jpgImage).'" alt="">';
}
But the image is corrupt most of the time . But to me the function seems ok. If I write it directly to the harddisc, the image looks just fine, but the base64-version is full with artefacts. Can anyone see what i am doing wrong?
$image
defined? You say the image is corrupt most of the time - under what conditions does it work or not work? You then say it is full of artefacts which is very different from being corrupt. Please clarify.