1

Want to take image from own server rotate certain angle and save the image.

Image file $filename = 'kitten_rotated.jpg'; With echo '<img src='.$filename.'>'; i see the image.

Then

$original = imagecreatefromjpeg($filename);
$angle = 90.0;
$rotated = imagerotate($original, $angle, 0);

Based on this https://stackoverflow.com/a/3693075/2118559 answer trying create image file

$output = 'google.com.jpg';

If i save the same image with new file name, all works

file_put_contents( $output, file_get_contents($filename) );

But if i try to save rotated image, then file_put_contents(): supplied resource is not a valid stream resource.

file_put_contents( $output, $rotated );

Here https://stackoverflow.com/a/12185462/2118559 read $export is going to be a GD image handle. It is NOT something you can simply dump out to a file and expect to get a JPG or PNG image.. but can not understand how to use the code in that answer.

How to create image file from $rotated?

Tried to experiment, based on this http://php.net/manual/en/function.imagecreatefromstring.php

$fh = fopen( 'some_name.png' , 'w') or die("can't open file");
fwrite($fh, $data );
fclose($fh);

Does it means that need something like

$data = base64_encode($rotated);

And then write in new file?

Community
  • 1
  • 1
Andris
  • 1,434
  • 1
  • 19
  • 34

3 Answers3

2

I have not tested this, but I think you need to encode the image as base 64 first.

If you check the string from any Image URL, you'd see data:image/png;base64, preceding the hash. Prepending this to your image string and saving.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
1

Here is a function that may help, based on what you already have:

// Function settings:
// 1) Original file
// 2) Angle to rotate
// 3) Output destination (false will output to browser)

function RotateJpg($filename = '',$angle = 0,$savename = false)
    {
        // Your original file
        $original   =   imagecreatefromjpeg($filename);
        // Rotate
        $rotated    =   imagerotate($original, $angle, 0);
        // If you have no destination, save to browser
        if($savename == false) {
                header('Content-Type: image/jpeg');
                imagejpeg($rotated);
            }
        else
            // Save to a directory with a new filename
            imagejpeg($rotated,$savename);

        // Standard destroy command
        imagedestroy($rotated);
    }

// Base image
$filename   =   'http://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg';

// Destination, including document root (you may have a defined root to use)
$saveto     =   $_SERVER['DOCUMENT_ROOT']."/images/test.jpg";

// Apply function
RotateJpg($filename,90,$saveto);
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • I just copy-paste your function and all works! Very good. Just need to understand how it works – Andris Dec 18 '14 at 08:23
  • Well I tried to notate as best as I could. Is there anything specific you are not sure of? – Rasclatt Dec 18 '14 at 08:24
  • The only thing was regarding `imagedestroy();` I commented it. If uncommented, then got `imagedestroy() expects exactly 1 parameter, 0 given `. As understand need to specify file which i want to delete? – Andris Dec 18 '14 at 08:29
  • 1
    There, sorry that was my fault. I forgot to reference the image file to destroy. – Rasclatt Dec 18 '14 at 08:30
  • One note. If need to rotate `png` or `gif`, then instead of `imagecreatefromjpg` need to use `imagecreatefrompng` or `imagecreatefromgif`... – Andris Dec 18 '14 at 08:40
  • 1
    Correct! You can build that into the function to recognize file types and adjust commands accordingly. – Rasclatt Dec 18 '14 at 08:41
  • Also you have to change output accordingly (`imagejpeg()`). – Rasclatt Dec 18 '14 at 08:42
1

If you want to save image just use one of GD library functions: imagepng() or imagepng().

imagerotate() returns image resource so this is not something like string.

In your case just save rotate image:

imagejpg($rotated, $output);

And now You can use $output variable as your new filename to include in view like before:

echo '<img src='.$output.'>';

Don't forget to include appropriate permissions in directory where You're saveing image.