php,autoresize图像正在运行。但它存储在本地,我想保存在单独的文件夹中

时间:2013-03-29 06:40:04

标签: php html mysql

这是我调整大小后的代码,图像直接存储在本地文件夹中。我已经更改为指定的文件夹。

    function imagecrop($img_name,$newname,$type,$modwidth,$modheight)
    {

list($width, $height) = getimagesize($img_name) ; //get width & height in array         list

    $tn = imagecreatetruecolor($modwidth, $modheight); 
if(!strcmp("image/png",$type))
{
imagealphablending($tn, false); //For transparent BackGround
imagesavealpha($tn, true);  
}



   if(!strcmp("image/jpg",$type) || !strcmp("image/jpeg",$type) || !strcmp("image/pjpeg",$type))
    $src_img=imagecreatefromjpeg($img_name);

    if(!strcmp("image/png",$type))
    $src_img=imagecreatefrompng($img_name);

    if(!strcmp("image/gif",$type))
        $src_img=imagecreatefromgif($img_name);

      imagecopyresampled($tn, $src_img, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 


       if(!strcmp("image/png",$type))  
       {
   imagesavealpha($src_img, true);
   $ok=imagepng($tn,$newname);
       }
   else if(!strcmp("image/gif",$type))  
       {
   $ok=imagegif($tn,$newname);
   }
       else 
   {
       $ok=imagejpeg($tn,$newname);
   }

    if($ok==1)
  {
    return "<img src=".$_FILES['userfile']['name']." border='0'>";
  }
    } 

2 个答案:

答案 0 :(得分:0)

保存时需要提供新图像的路径。请尝试以下代码

function imagecrop($img_name,$newname,$type,$modwidth,$modheight)
{

    $newname = "/home/public_html/images/$newname"; //just an example


    list($width, $height) = getimagesize($img_name) ; //get width & height in array         list

    $tn = imagecreatetruecolor($modwidth, $modheight); 
    if(!strcmp("image/png",$type))
    {
       imagealphablending($tn, false); //For transparent BackGround
       imagesavealpha($tn, true);  
    }



   if(!strcmp("image/jpg",$type) || !strcmp("image/jpeg",$type) || !strcmp("image/pjpeg",$type))
    $src_img=imagecreatefromjpeg($img_name);

    if(!strcmp("image/png",$type))
    $src_img=imagecreatefrompng($img_name);

    if(!strcmp("image/gif",$type))
        $src_img=imagecreatefromgif($img_name);

      imagecopyresampled($tn, $src_img, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 


       if(!strcmp("image/png",$type))  
       {
   imagesavealpha($src_img, true);
   $ok=imagepng($tn,$newname);
       }
   else if(!strcmp("image/gif",$type))  
       {
   $ok=imagegif($tn,$newname);
   }
       else 
   {
       $ok=imagejpeg($tn,$newname);
   }

    if($ok==1)
  {
    return "<img src=".$_FILES['userfile']['name']." border='0'>";
  }
    } 

答案 1 :(得分:0)

在输出图像函数的第二个参数中添加目标路径(imagepng,imagegif,imagejpeg)。从PHP手册:

  

filename - 保存文件的路径。如果未设置或NULL,则为raw   图像流将直接输出。