php 调整图像大小并使用 imagejpeg() 保存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17121835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:18:18  来源:igfitidea点击:

Resize an image and save with imagejpeg()

phpresize

提问by Ponch0

iam trying to resize an imageto a thumbnail with php! I get no errors but it won't save the thumbnail on my server. The code is:

我正在尝试使用 php 将图像大小调整为缩略图!我没有收到任何错误,但它不会将缩略图保存在我的服务器上。代码是:

#Resize image
function resize($input_dir, $cur_file, $newwidth, $output_dir)
{
    $filename = $input_dir.'/'.$cur_file;
    $format='';
    if(preg_match("/.jpg/i", $filename))
    {
        $format = 'image/jpeg';
    }
    if (preg_match("/.gif/i", $filename))
    {
        $format = 'image/gif';
    }
    if(preg_match("/.png/i", $filename))
    {
        $format = 'image/png';
    }
    if($format!='')
    {
        list($width, $height) = getimagesize($filename);
        $newheight=$height*$newwidth/$width;
        switch($format)
        {
            case 'image/jpeg':
            $source = imagecreatefromjpeg($filename);
            break;
            case 'image/gif';
            $source = imagecreatefromgif($filename);
            break;
            case 'image/png':
            $source = imagecreatefrompng($filename);
            break;
        }
        $thumb = imagecreatetruecolor($newwidth,$newheight);
        imagealphablending($thumb, false);
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($thumb, 'thumb_'.$cur_file);
    }
}

My function looks like:

我的功能看起来像:

resize(plugins_url().'/MyImagePlugin/img', 'testimg.jpg', "200");

The Image is in the Folder "img" in my Plugin Dic. The wired thing is ,that i don't get any erros?! CHMOD is 777 for img-folder.

图像位于我的插件 Dic 中的文件夹“img”中。有线的事情是,我没有得到任何错误?!img 文件夹的 CHMOD 为 777。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Ian Kenney

You are not using any path when you save the image in the line:

在行中保存图像时,您没有使用任何路径:

imagejpeg($thumb, 'thumb_'.$cur_file);

$cur_fileis set to testing.jpg.

$cur_file设置为 testing.jpg。

You should add the path to the filename or it will be attempoting to create it in whatever the current directory is.

您应该将路径添加到文件名,否则它将尝试在当前目录中的任何目录中创建它。

Changes are something like :

变化是这样的:

function resize($input_dir, $cur_file, $newwidth, $output_dir = "" )
{
   if($output_dir == "") $output_dir = $input_dir;

   .....

      imagejpeg($thumb, $output_dir.'/thumb_'.$cur_file);
    }
}

回答by lakshya_arora

I know that this is not your way of doing but iam sure it can help you.

我知道这不是你的方式,但我相信它可以帮助你。

if (isset($_FILES['image']['name']))
 {
  $saveto = "dirname/file.jpg";
  move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
  $typeok = TRUE;
  switch($_FILES['image']['type'])
  {
  case "image/gif": $src = imagecreatefromgif($saveto); break;
  case "image/jpeg": // Both regular and progressive jpegs
  case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
  case "image/png": $src = imagecreatefrompng($saveto); break;
  default: $typeok = FALSE; break;
  }
  if ($typeok)
  {
  list($w, $h) = getimagesize($saveto);
  $max = 500; 
  \ you can change this to desired product for height and width.
  $tw = $w;
  $th = $h;
  if ($w > $h && $max < $w)
  {      
  $th = $max / $w * $h;
  $tw = $max;
  }
  elseif ($h > $w && $max < $h)      
  {
  $tw = $max / $h * $w;
  $th = $max;
  }
  elseif ($max < $w)
  {
  $tw = $th = $max;
  }
  $tmp = imagecreatetruecolor($tw, $th);      
  imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
  imageconvolution($tmp, array( // Sharpen image
  array(-1, -1, -1),
  array(-1, 16, -1),
  array(-1, -1, -1)
  ), 8, 0);
  imagejpeg($tmp, $saveto);
  imagedestroy($tmp);
  imagedestroy($src);
  }
  }