1

I have to create thumbnail image or medium image from the original image I am using below code to resize an image because when I upload a portrait image it create thumbnail image but it rotates the image that is creating a problem for me. I have to stop rotation of an image after resizing an image and I am not getting why it is because below code is working fine in landscape mode. In landscape image is resizing fine it is not rotating after resizing an image but in portrait images, it is not working well please guide me.
Below is the code

<?php

error_reporting(0);

//Start code to create thumb image


          $new_images = $_FILES['userfiles']['name'];
          $extension=explode('.',$new_images);
          $extensionname=$extension[1];
          $image=time().$new_images;
          $images=$_FILES['userfiles']['tmp_name'];


        move_uploaded_file($_FILES,"image/".$image);
        $width=300;
        $height=300;

        // Fix Width & Heigh (Auto calculate)
          //  var_dump($images);die;
        $size=GetimageSize($images);
        var_dump($size);
        $height=round($width*$size[1]/$size[0]);

        if($extensionname=="jpg" || $extensionname=="jpeg")
        {
            $images_orig = ImageCreateFromJPEG($images);
        }
        elseif($extensionname=="png")
        {
            $images_orig = imagecreatefrompng($images);
        }
        else
        {
            $images_orig = imagecreatefromgif($images);
        }
        $photoX = ImagesX($images_orig);
        $photoY = ImagesY($images_orig);
        $images_fin = ImageCreateTrueColor($width, $height);
        $transparent=imagecolorallocatealpha($images_fin,255, 255, 255, 127);
        imagealphablending($images_fin, false);
        imagesavealpha($images_fin,true);
        imagefilledrectangle($newImg, 0, 0, $photoX, $photoY, $transparent);
        ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);

        if($extensionname=="jpg" || $extensionname=="jpeg")
        {
            imagejpeg($images_fin,"image/".$image);
        }
        elseif($extensionname=="png")
        {
            imagepng($images_fin,"image/".$image);
        }
        else
         {
            imagegif($images_fin,"image/".$image);
         }

        //var_dump($new_images);
        ImageDestroy($images_orig);
        ImageDestroy($images_fin);

Thanks in Advance 
Kindle Q
  • 944
  • 2
  • 19
  • 28
varun joshi
  • 461
  • 1
  • 8
  • 27
  • see http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ and http://stackoverflow.com/questions/7489742/php-read-exif-data-and-adjust-orientation – VolkerK Nov 30 '15 at 07:27
  • I am not getting how to apply this in my code – varun joshi Nov 30 '15 at 07:31
  • Are you sure this code is working fine? Here `move_uploaded_file($_FILES,"image/".$image);`, it expects a string but you're passing an array. Your `$newImg` variable is undefined in `imagefilledrectangle($newImg, 0, 0, $photoX, $photoY, $transparent);` – Rajdeep Paul Nov 30 '15 at 07:39
  • yes it getting an error but it uploaded a file in folder thats not the issue that i will handle. – varun joshi Nov 30 '15 at 07:44
  • hi i have use this function to rotate image again with same position $images_fin = imagerotate($images_fin, 270, 0); but i am having one problem how to find image is landscape or potrait for this i have used getimagesize($_FILES["imagefile"]["tmp_name"] but in both mode landscape as well as in potrait it give maximum width it is getting difficult for me to find image is potrait or landscape. – varun joshi Nov 30 '15 at 08:39
  • 1
    Finally i have done it yupiee this link is quite beneficial to find in which way the image is rotating http://www.kendawson.ca/2012/09/24/php-image-rotation-iphone-portrait-exif-orientation-gd2-imagemagick-libraries/ – varun joshi Nov 30 '15 at 10:16

0 Answers0