dping1968 2013-08-31 11:00
浏览 9
已采纳

图像调整大小与PHP获取错误

I am working on project its about gemstone where I make admin panel where user can upload images am trying to resize the images it work some how but it give me black output in given dir where I am uploading . Here is my code .

 $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;
        } 
  • 写回答

1条回答 默认 最新

  • dqs13465424392 2013-09-03 18:41
    关注

    First of all change the imagecopyresampled function to imagecopyresized function.

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

    Than change the function from imagecreatefromgif to imagecreatefromjpeg because you are using jpg in if condition.And write the path below i have given like that.

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

    And sure to unlink if you don't than it will bring you two image files because you are using copy function be sure to unlink.

       unlink($this->filename);
    

    I have just done it. I hope it will work for you too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口