1

I have a requirement to upload image on server in 2 size, normal and small size.

So i am sending http request to php file, i am successfully able to upload Image on server in normal form,

But when i am trying to resize and then uploading it on server, it is not uploading..

So here is my php script

<php

define ("MAX_SIZE","400");

if($_SERVER["REQUEST_METHOD"] == "POST")
{
   $image =$_FILES['userfile']['name'];
   $uploadedfile = $_FILES['userfile']['tmp_name'];

if ($image) 
{

    $filename = stripslashes($_FILES['userfile']['name']);

    $extension = getExtension($filename);
    $extension = strtolower($extension);

            if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
    {
        $errors=1;
    }
    else
    {
                    $size=filesize($_FILES['userfile']['tmp_name']);
                    if ($size > MAX_SIZE*1024)
                    {
                       $errors=1;
                    }
                    if($extension=="jpg" || $extension=="jpeg" )
                    {
                           $uploadedfile = $_FILES['userfile']['tmp_name'];
                           $src = imagecreatefromjpeg($uploadedfile);
                    }


                    list($width,$height)=getimagesize($uploadedfile);

                    $newwidth = 70;                  
                    $newheight = 70;

                    $tmp = imagecreatetruecolor($newwidth,$newheight);

                    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);



                    $file_name = substr ( md5(uniqid(rand(),1)), 5, 15); 


                    $iconuploaddir = "/home/announce/public_html/TravelApp/images/$output/icons/";

                    $file = basename($_FILES['userfile']['name']);
                    $newname = $file_name . $file;

                    $uploadIconfile = $iconuploaddir . $newname;


                    if (move_uploaded_file($temp, $uploadIconfile)) {
                           $imagepath =  $baseuploaddir;// Give the path where the image saves. or print some messages
                    }

                    imagedestroy($src);
                    imagedestroy($tmp);                      
           }
        }
 }
?>

i guess move_uploaded_file($temp, $uploadIconfile)) is the main cause of problem

please help me.

2
  • why someone wold like to read lots of partly commented code? Commented Jan 18, 2012 at 11:33
  • @k102 because he is asking for help Commented Jan 18, 2012 at 11:40

1 Answer 1

2

You cannot be using move_uploaded_file for the thumbnail because the thumbnail file was not uploaded

You can use this function to create the thumbnail after calling move_uploaded_file for the file that was uploaded

function make_thumb($src,$dest,$desired_width)
{

  /* read the source image */
  $source_image = imagecreatefromjpeg($src);
  $width = imagesx($source_image);
  $height = imagesy($source_image);

  /* find the "desired height" of this thumbnail, relative to the desired width  */
  $desired_height = floor($height*($desired_width/$width));

  /* create a new, "virtual" image */
  $virtual_image = imagecreatetruecolor($desired_width,$desired_height);

  /* copy source image at a resized size */
  imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);

  /* create the physical thumbnail image to its destination */
  imagejpeg($virtual_image,$dest);
}

For the $src param pass $imagepath and for $dest, put the destination you want the thumbmail image to be saved.

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

3 Comments

when i tried to use this function, PHP Warning: imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]: Unable to open '/home/announce/public_html/TravelApp/images/TajMahal/icons/' for writing: Is a directory in /home/announce/public_html/TravelApp/php/test-upload.php.. Whta the error??
change the write permission of the directory to 777 or something that can enable you to write to it
Hey i am able to solve it out, i googled it found out that path to $dest needs to be correct.So after doing it it was successfully uploaded.Thanks a lot @yankitwizzy

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.