1

我正在研究关于 gemstone 的项目,我在其中制作管理面板,用户可以在其中上传图像正在尝试调整图像的大小,但它在我上传的给定目录中给了我黑色输出。这是我的代码。

 $target_path = SITE_ROOT .DS. $this->upload_dir .DS. $this->filename;

      // Make sure a file doesn't already exist in the target location
      if(file_exists($target_path)) {
        $this->errors[] = "The file {$this->filename} already exists.";
        return false;
      }
      // i am trying to resize the image
     if( copy ($this->temp_path,$this->filename) or die     ("Could not copy")){
     $imagefile=$this->filename;
   list($width, $height) = getimagesize($this->filename);
   $image_p = imagecreatetruecolor($this->new_width,$this->new_height);
   if ($this->type =='jpg') 
   {
       $img = imagecreatefromgif($imagefile);
       imagecopyresampled($image_p, $img, 0, 0, 0, 0, $this->new_width,$this->new_height, $width, $height);
       imagegif($image_p,$target_path);
   }
   else{ echo 'something went wrongs';}
   }
        // Attempt to move the file 
        if(move_uploaded_file($this->temp_path, $target_path)) {
        // Success
            // Save a corresponding entry to the database
            if($this->create()) {
                // We are done with temp_path, the file isn't there anymore
                unset($this->temp_path);
                return true;
            }
        } else {
            // File was not moved.
        $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
        return false;
        } 
4

1 回答 1

2

首先将 imagecopyresampled 函数更改为 imagecopyresized 函数。

 imagecopyresized($image_p, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height);

比将函数从 imagecreatefromgif 更改为 imagecreatefromjpeg 因为您在 if 条件下使用 jpg。然后在下面写下我给出的路径。

  move_uploaded_file($this->temp_path, imagejpeg($image_p,$target_path,100));

如果您不这样做,请务必取消链接,因为您正在使用复制功能,所以它会给您带来两个图像文件,请务必取消链接。

   unlink($this->filename);

我刚刚做到了。我希望它也对你有用。

于 2013-09-03T18:41:35.083 回答