douyanyan1123 2011-07-22 13:02
浏览 75
已采纳

如何更改存储文件的名称

I am using image upload and resize script from someones blog. It stores the name of file as resize.jpg .however i want to give unique names to them as data will go in database...I am very bad in using functions so please guide me.

 <?php
    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;
   }      

}
?>  
<?php
   if( isset($_POST['submit']) ) {
      include('SimpleImage.php');
      $image = new SimpleImage();
      $image->load($_FILES['uploaded_image']['tmp_name']);
      $image->resizeToWidth(300);
      $image->resizeToHeight(200);
      $image->save('images/resize.jpg');
      //$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
   }
?>
  • 写回答

2条回答 默认 最新

  • douchen4915 2011-07-22 13:17
    关注

    Inside this block, you are saving every one as resize.jpg

      $image->resizeToWidth(300);
      $image->resizeToHeight(200);
      $image->save('images/resize.jpg');
    

    You can generate a unique name with uniqid() and optionally add a prefix onto it like img_. Your filenames will then look like:

    'img_4e297753130db.jpg'
    

    Start by generating a filename, saved in a variable.

    $prefix = "img_"
    $new_filename = uniqid($prefix) . ".jpg";
    
    // Do your other processing
    // ...
    $image->resizeToWidth(300);
    $image->resizeToHeight(200);
    
    // Save with the new filename
    // Note change to double quotes from single...
    $image->save("images/$new_filename");
    

    Later, when you're ready to store the filename in the database, it's still available in $new_filename

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)