imagerotate not working

I spent a week on this, I cannot for the life of me get Imagerotate to work! The software code from a big shipping company sends my server a label image that looks like this:

echo ‘’;

I needed 2 things done:
1.) The image saved
2.) The image displayed rotated.

So I thought I’d save it in my label directory, first, with these 2 lines of code:

$rawData=base64_decode($label_image);
file_put_contents(‘labels/’. $unique_id . ‘.png’, $rawData);

Am I doing at least that part right? It seems to work.

But then… I can’t get it to rotate! I’ve tried every permutation of “imagerotate()” without it working. Ugh!

And, also, I don’t know if I should rotate it first, before saving… or, save it as is, and then rotate the displayed image.

I could get the image to rotate using CSS… that’s no problem. But I would rather use PHP, and not CSS.

Would someone be kind enough to provide the line(s) of code to make this work for this dumb tennessee girl who’s trying to learn PHP in a man’s world? :o

What does your attempt look like?

[php]

<?php // File and rotation $filename = 'test.jpg'; $degrees = 90; // Content type header('Content-type: image/jpeg'); // Load $source = imagecreatefromjpeg($filename); // Rotate $rotate = imagerotate($source, $degrees, 0); // Output imagejpeg($rotate); ?>

[/php]

Does this work for you?

It’s the “header” thingy that flubs everything up. See, when you do php “header,” the entire browser page is just the image. What I am wanting is for the rotated image to be nestled in with several paragraphs.

The image is actually a shipping label. So my webpage will look like this:

Hello! Welcome! Here is your shipping label to
print. Remember to affix it securely to your box.
Then you can take it to any shipping center.
Here is your shipping label:

------------
------------

And. just as a reminder, use lots of
tape on the label and on the box
and blah blah blah blah.

Okay, see what I mean? If I do the header thing, then the only thing on the whole page can only be the image, and I am not wanting that. I am wanting the image to appear inside a paragraph of words. So my html code needs to look like this:
echo ‘Hello and welcome. Here
is your shipping label. Be sure
to be careful when cutting it. Here
is the label, right here:

’;
echo ‘’;
echo ‘

And then you can
take your package to any place
on the planet and ship it.’;

I’m not sure if it’s best to use PHP to rotate the image, first, then save it. Then call the saved image? Or is it better to save the raw, non-rotated image, but have php rotate it on the fly?

Again, I know how to rotate it using CSS… but I am wanting to make PHP do the rotating…

Thank you again! I hope you can help. I really appreciate it!!

I would have a link open the label in a new window. When you print the label, you don’t want the other text on it. The other option, is to have a print only stylesheet that hides all text, but the label for when it is printed.

Okay, but back to my question – I want the label to appear mixed in with an html page (not on its own page) and Yes I will use CSS so nothing gets printed.

But, again, how do I get imagerotate to work? Everyone keeps giving me the same code “blah blah blah header header HEADER” but I do NOT want the label on its own page! Having a header:png makes it so ONLY it will appear on own page.

I want it mixed in. Mixed in. mixed in.

Is this not possible/

So you are after something like this?

ImageRotate


Yes! Exactly! How did you do that?

The worker file that handles the image modifications.

[php]<?php

// if r is not set, it shows nothing.
if ( isset( $_GET[‘r’]) )
{
// check if file exists. If it does, return it. If not show a message
if ( file_exists($_GET[‘r’])) {

    // use htmlentities in case it is malicious
    $filename = htmlentities($_GET['r']);
    $degrees = 90; // rotate the image 90 degree's
    
    // modify the content type to the image format you receive
    header('Content-type: image/jpeg');
    
    $source = imagecreatefromjpeg($filename);
    $rotate = imagerotate($source, $degrees, 0);

    // Output
    imagejpeg($rotate);
} else {
    echo "Image not Found.";
}

}
[/php]

The display file:
[php]<?php
/**

  • Created by PhpStorm.
  • User: Andrew
  • Date: 7/6/2015
  • Time: 3:29 PM
    */

?>

iframe { width: 300px; height: 325px; } div { width: 300px; margin: 10px auto; }

This is my imageRotate example:

Not ideal, but works

[/php]

Obviously, you need to modify this to suit, but it does what you are asking.

While I would buy you a beer for taking the time and trouble out of your day to help a complete stranger on the 'net, I’ll “pass” on your answer, because it’s cheating. Cheating, like using CSS.

I wanted to know if there was a way to make PHP rotate an image, but my goodness, I have not seen anything more complicated/challenging than when the Wright Brothers had to invent a plane.

You would think you could just go “imagerotate(stupid-photo.jpg)” and voila! Image rotated!

But no. 80 billion hoops to have to go through.

And I’m not mad at you, I am very appreciative that you tried. I am just mad that PHP can’t do something so simple without having to have the image on its own page.

I thought I made it clear in all of my posts: no css, and no headers.

But I guess PHP simply cannot do what I ask, and that is why all of these workarounds.

Anyway, thank you again for your help. Have a great day.

My suggestion, that is still a work around, but what you likely need anyway.

Rotate the image and save the file. Then output the new image.

Sorry. Don’t know if it will work, but you could try this as well,
Rotate Image on same page(SO)

Christy, did you test Cipher’s code? He gave you the simple three lines that will rotate your picture in PHP.

     $source = imagecreatefromjpeg($filename);
     $rotate = imagerotate($source, $degrees, 0);

     // Output
     imagejpeg($rotate);

ANother sample using file-output to save the file…
//define image path
$filename=“image.jpg”;

// Load the image
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

//and save it on your server…
file_put_contents(“myNEWimage.jpg”,$rotate);

You just alter the rotate arguments to your needed options, $degrees, like 45 or 90 degrees…
Just look at the imagerotate() function’s to set it up correctly…
http://php.net/manual/en/function.imagerotate.php
Look at the examples to see how to do it. And, it shows how to destroy your work variables so that it
does not take up too much memory on the server…

Use astonecipher’s routine and it will work for you!

Sponsor our Newsletter | Privacy Policy | Terms of Service