0

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

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

2 回答 2

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'>";
  }
    } 
于 2013-03-29T07:24:43.790 回答
0

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

文件名- 保存文件的路径。如果不设置或为NULL,将直接输出原始图像流。

于 2013-03-29T07:27:15.193 回答