Paul
Paul

Reputation: 11746

Unable to rotate image with PHP's imagerotate

I continue to get this error after trying to retrieve an image from my DB and rotate it:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format

Here is what I'm calling to convert the blob data back into an image for rotation:

$SQL="SELECT * FROM images WHERE id={$id}";
$rh = mysql_query($SQL);

$image=mysql_result($rh,0,"image");
$source_image=imagecreatefromstring($image);

$rotate_image = imagerotate($source_image, 90, 0);

Am I missing a step?

Upvotes: 0

Views: 740

Answers (2)

Paul
Paul

Reputation: 11746

This is what I ended up doing for it to work:

$SQL="SELECT * FROM images WHERE id={$id}";
$rs = mysql_query($SQL);

//rotate image
$image=mysql_result($rs,0,"image");
$source_image=imagecreatefromstring($image);

$rotate_image = imagerotate($source_image, 270, 0);

ob_start();
imagejpeg($rotate_image, null, 100);
$image_bin = mysql_real_escape_string(ob_get_contents()); data.
ob_end_clean(); 

Upvotes: 2

David Bélanger
David Bélanger

Reputation: 7438

I recommend to store image data in base64. No problem with storing character and binary.

Then it's easy as that :

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

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

Source : http://php.net/manual/fr/function.imagecreatefromstring.php

Upvotes: 0

Related Questions