Results 1 to 2 of 2

Thread: trouble rotating image

  1. #1

    trouble rotating image

    i can't get the read_exif_blablab whatever to work so i created a entry in the database to inform the page of the orientation of the image, if the value is one then the image needs to be rotated 90 degrees, well i got it to work when i declare the image name as plain text but when i try to get it to read it in a variable all hell breaks loose image turns into a text nightmare. is their a way to get this to work? here is the source code

    PHP Code:
    if($temp[6]==0){
            echo 
    '<img src='.'"'.$temp[2].'"'.">";
            }else{
            
            
    $image $temp[1];

    //How many degrees you wish to rotate
    $degrees 90;
    // Create the canvas
    $source imagecreatefromjpeg("italy/".$image) ;

    // Rotates the image
    $rotate imagerotate($source$degrees0) ;

    // Outputs a jpg image, you could change this to gif or png if needed
    imagejpeg($rotate) ; 

  2. #2
    The imagejpeg (and its cousins) function sends binary straight to the output buffer (the web browser). That won't work unless you tell the web browser that the content you're giving it is an image by setting the Content-Type header.

    In your case, you're sending the web browser an HTML file, so you have to write your freshly rotated image to the hard disk first. Then you can refer to it in an IMG tag in your HTML. The key is that PHP must have write privileges on the directory you want to write your rotated image to, and that directory must be accessible from your website's document root.

    Here's an example that might help...

    PHP Code:
    $image_file "italy.jpg";
    $images_root $_SERVER['DOCUMENT_ROOT'] . "/photos";
    $image_path $images_root "/" $image_file;

    // Debugging...
    if (!is_file($image_path))
    {
      exit(
    "Can't find image at " $image_path);
    }

    $rotate $_GET['rotate'];
    // Pass degrees to rotate the image on the URL as ?rotate=
    if (!empty($rotate) && is_numeric($rotate) && ($rotate >= && $rotate <= 360))
    {
      
    $image_handle imagecreatefromjpeg($image_path);

      
    // Debugging...
      
    if (!$image_handle)
      {
        exit(
    "GD could not load image into memory: " $image_path);
      }
      
      
    // Rotate the image -- remember this is done in memory
      
    $rotated_image_handle imagerotate($image_handle$rotate0);

      
    // Now we need to write the rotated image out to the disk
      
      // First, we need a file name to write to and we must be able
      // to write to the $images_root directory
      
    $rotated_image_path tempnam($images_root"") . ".jpg";
      
      
    // Now, write the image to disk
      
    $success imagejpeg($rotated_image_handle$rotated_image_path80);

      
    // Debugging...
      
    if (!$success)
      {
        exit(
    "GD could not write rotated image to disk");
      }
      
      
    // Now we can refer to the new file we wrote to disk in our HTML
      
    echo "<img src=\"photos/".basename($rotated_image_path)."\" />";
    }
    else
    {
      echo 
    "<img src=\"photos/".$image_file."\" />";


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012