dsjgk330337 2014-08-23 15:37
浏览 19

如何重新调整高质量图像使用此代码?

How to re-size image with high quality When using this code ?

.....................................................................................................................................................................

<?PHP
if(isset($_POST["Submit"]))
    {
        $image_type_check = $_FILES['file']['type']; //file type
        $image_name = $_FILES['file']['name']; //file name
        $image_size = $_FILES['file']['size']; //file size
        $image_temp = $_FILES['file']['tmp_name']; //file temp
        $image_size_info = getimagesize($image_temp); //get image size

        //Get file extension and name to construct new file name 
        $image_info = pathinfo($image_name);
        $image_extension = strtolower($image_info["extension"]); //image extension
        $image_name_only = strtolower($image_info["filename"]);//file name only, no extension

        //create a random name for new image (Eg: fileName_293749.jpg);
        $new_file_name = '123456789'.$image_extension;

        list($width,$height)=getimagesize($image_temp);

        $newwidth=985;
        $newheight=($height/$width)*$newwidth;
        $tmp=imagecreatetruecolor($newwidth,$newheight);

        $filename = "uploads/".$new_file_name;

        imagejpeg($tmp,$filename,100);
    }
?>
  • 写回答

1条回答 默认 最新

  • douqiao6015 2014-08-23 17:05
    关注
    • you should use move_uploaded_file()
      to make sure that the paths are OK and the file is valid .

    • To change the size you need imagecopyresampled() .

    • your file name is invalid with

    $image_info = pathinfo($image_name);
    $image_extension = strtolower($image_info["extension"]);
    $new_file_name = '123456789'.$image_extension;

    you get

    $new_file_name == '123456789jpg'
    without .


    [...]
    $image_extension = "." . strtolower($image_info["extension"]);
    
    $target = "uploads/123456789".$image_extension;
    
    if(move_uploaded_file($image_temp,$target)) {
      if ($image_extension == '.jpg') {
      // Get new dimensions
      list($width_orig, $height_orig) = getimagesize($image_temp);
      $ratio_orig = $width_orig/$height_orig;
    
      $width = 985;
    
      if ($width_orig > $height_orig) {
        $height = $width / $ratio_orig;
      } else {
        $height = $width * $ratio_orig;
      }   
    
      // Resample
    
      $image_p = imagecreatetruecolor($width, $height);
      $image = imagecreatefromjpeg($target);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    
      imagejpeg($image_p,$target,100);
      imagedestroy($image_p);
      } //is jpg file
    } else { echo "File could not be moved to" .$target; }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?