douzhao7634 2010-02-23 08:11
浏览 48

用PHP调整图像大小

I have script to resize image. It work well except output. In out put it gives binary stream but i want image . my code is following

class SimpleImage {

    var $image;
   var $image_type;

 function load($filename) {
    $image_info = getimagesize($filename);
  $this->image_type = $image_info[2];
  if( $this->image_type == IMAGETYPE_JPEG ) {
     $this->image = imagecreatefromjpeg($filename);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {
     $this->image = imagecreatefromgif($filename);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {
     $this->image = imagecreatefrompng($filename);
  }
  }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image,$filename,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {
     imagegif($this->image,$filename);         
  } elseif( $image_type == IMAGETYPE_PNG ) {
     imagepng($this->image,$filename);
  }   
  if( $permissions != null) {
     chmod($filename,$permissions);
  }
  }
  function output($image_type=IMAGETYPE_JPEG) {
  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {
     imagegif($this->image);         
  } elseif( $image_type == IMAGETYPE_PNG ) {
     imagepng($this->image);
  }  

 }
  function getWidth() {
  return imagesx($this->image);
   }
   function getHeight() {
  return imagesy($this->image);
   }
   function resizeToHeight($height) {
  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
  }
  function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
   }
     function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100; 
  $this->resize($width,$height);
     }
       function resize($width,$height) {
          $new_image = imagecreatetruecolor($width, $height);
         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height,$this->getWidth(), $this->getHeight());
       $this->image = $new_image;   
        }      
     }
    ?>

and

   <?php
         if( isset($_POST['submit']) ) {
          include('SimpleImage.php');
         $image = new SimpleImage();
         $image->load($_FILES['uploaded_image']['tmp_name']);
              $image->resizeToWidth(400,400);
          $image->save($_FILES['uploaded_image']['name']);

         $image->output();
           } else {
       ?>

      <form action="" method="post" enctype="multipart/form-data">
       <input type="file" name="uploaded_image" />
       <input type="submit" name="submit" value="Upload" />
     </form>

       <?php
               }
              ?>
  • 写回答

3条回答 默认 最新

  • dongmei9508 2010-02-23 08:33
    关注

    Set the content type header at the beginning of your php file, so that the browser understands that an image is to be displayed. (Use image/png for png files).

    header('Content-type: image/jpeg');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型