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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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);

7 Comments

I just copy-paste your function and all works! Very good. Just need to understand how it works
Well I tried to notate as best as I could. Is there anything specific you are not sure of?
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?
There, sorry that was my fault. I forgot to reference the image file to destroy.
Correct! You can build that into the function to recognize file types and adjust commands accordingly.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.