Php,自动调整图像大小工作.但它存储在本地,我想保存在单独的文件夹中


php,autoresize image is working. but it stored in local, i want to save in separate folder

这是我的代码调整后的图像直接存储在本地文件夹。我已更改到指定文件夹

    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'>";
  }
    } 

保存时需要给出新图像的路径。试试下面的代码

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'>";
  }
    } 

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

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

相关文章: