Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:48:25+00:00 2026-06-13T00:48:25+00:00

I’ve been working on a system to rotate an uploaded image. The algorithm works

  • 0

I’ve been working on a system to rotate an uploaded image. The algorithm works as follows:

 1) User uploads a jpeg.  It gets saved as a PNG
 2) Link to the temp png is returned to the user.
 3) The user can click 90left,90right, or type in N degrees to rotate
 4) The png is opened using 

   $image = imagecreatefrompng("./fileHERE");

 5) The png is rotated using

   $imageRotated = imagerotate($image,$degrees,0);

 6) The png is saved and the link returned to the user.
 7) If the user wishes to rotate more go back to step 3 operating on the newly
    saved temporary PNG, 
    else the changes are commited and the final image is saved as a jpeg.

This works perfectly fine when rotating 90degrees left and right. The user can rotate infinity many times without any loss in quality. The issue is that when the user tries to rotate 20 degrees (or some other non multiple of 90). When rotating 20 degrees the image is rotated slightly and a black box forms to fill the areas that need to be filled. Since the image (with the black box) is saved as a png the next rotate of 20 degrees rotates the image (with the black box) another 20 degrees and forms another black box to take up the slack. Long story short if you do this to 360 degrees you will have a large black box around a very small remaining image. Even if you zoom in and crop out the black box there is a noticeable loss in quality.

Any way I can avoid the black box? (The server does not have imagick installed)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:48:26+00:00Added an answer on June 13, 2026 at 12:48 am

    Always store the source file unmodified and when you rotate, rotate the number of degrees using the original source file. so 20 degress + 20 degress, means rotate the source 40 degrees.

    1. User uploads a JPEG.
    2. The user can click “90 left”, “90 right”, or type in N degrees to rotate.
    3. The png is opened using

      $image = imagecreatefromjpeg("./source.jpg");
      
    4. The png is rotated…

      // If this is the first time, there is no rotation data, set it up
      if(!isset($_SESSION["degrees"])) $_SESSION["degrees"] = 0;
      
      // Apply the new rotation
      $_SESSION["degrees"] += $degrees;
      
      // Rotate the image
      $rotated = imagerotate($image, $_SESSION["degrees"], 0);
      
      // Save the image, DO NOT MODIFY THE SOURCE FILE!
      imagejpeg($rotated, "./last.jpg");
      
      // Output the image
      header("Content-Type: image/jpeg");
      imagejpeg($rotated);
      
    5. If the user wishes to rotate more go back to step 3, otherwise the last.jpg is taken as final and the $_SESSION["degrees"] parameter is destroyed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I am using Paperclip to handle profile photo uploads in my app. They upload
I need to clean up various Word 'smart' characters in user input, including but
Let's say I'm outputting a post title and in our database, it's Hello Y’all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.