Php脚本无法在服务器上上传图像

时间:2012-01-18 11:27:08

标签: php iphone ios image

我要求在服务器上上传2个大小,正常和小尺寸的图像。

所以我发送http请求到php文件,我成功地能够以正常形式上传服务器上的Image,

但是当我尝试调整大小然后将其上传到服务器上时,它无法上传..

所以这是我的php脚本

<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);                      
           }
        }
 }
?>

我猜move_uploaded_file($ temp,$ uploadIconfile))是问题的主要原因

请帮帮我。

1 个答案:

答案 0 :(得分:2)

您无法将move_uploaded_file用于缩略图,因为缩略图文件未上传

您可以在调用move_uploaded_file上传文件后使用此功能创建缩略图

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);
}

对于$src param pass $imagepath$dest,请输入您想要保存thumbmail图像的目的地。

相关问题