0

我有一个脚本可以完美地调整 .JPG 图像的大小,它完全可以做到它应该做的事情。但是,我想为用户提供同时添加 .PNG 和 .GIF 图像的选项。有人可以帮我扩展以下脚本,使其也支持这些格式吗?提前致谢!

$image = $_POST['bedrijfslogo'];
$new_image = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg";
copy($_POST['bedrijfslogo'],"copylogo.jpg");
$width=250; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($image);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($image);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin, $new_image);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
unlink($image);
4

3 回答 3

0

根据我的评论,我意识到您可以从返回的数据中提取图像的类型getimagesize()

试试这个,它应该接受您的 PHP 实例能够理解的任何类型的图像(未经测试):

// Get file paths
// Taking a local file path direct from the user? This is a *big* security risk...
$srcPath = $_POST['bedrijfslogo'];
$dstPath = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg";

// Not really sure why this is here, you don't seem to use the copy of the file
copy($srcPath, "copylogo.jpg");

// Get information about source image
$info = getimagesize($srcPath);
$srcWidth = $info[0];
$srcHeight = $info[1];
$srcType = $info[2];

// Calculate new width and height
// Width is fixed, height calculated from width
$dstWidth = 250;
$dstHeight = round($dstWidth * $srcHeight / $srcWidth);

// Get the image types supported by this instance of PHP
$supportedTypes = imagetypes();

// Create the source image resource based on it's type
switch ($srcType) {
  case IMG_GIF:
    if ($supportedTypes & IMG_GIF) {
      $srcImage = imagecreatefromgif($srcPath);
    } else {
      // GIF support not enabled on your PHP instance. Handle error here.
      exit('GIF support not enabled on this PHP instance');
    }
    break;
  case IMG_JPG: case IMG_JPEG: // Think these might have the same value?
    if ($supportedTypes & IMG_JPG) {
      $srcImage = imagecreatefromjpeg($srcPath);
    } else {
      // JPEG support not enabled on your PHP instance. Handle error here.
      exit('JPEG support not enabled on this PHP instance');
    }
    break;
  case IMG_PNG:
    if ($supportedTypes & IMG_PNG) {
      $srcImage = imagecreatefrompng($srcPath);
    } else {
      // PNG support not enabled on your PHP instance. Handle error here.
      exit('PNG support not enabled on this PHP instance');
    }
    break;
  case IMG_WBMP:
    if ($supportedTypes & IMG_WBMP) {
      $srcImage = imagecreatefromwbmp($srcPath);
    } else {
      // WBMP support not enabled on your PHP instance. Handle error here.
      exit('WBMP support not enabled on this PHP instance');
    }
    break;
  case IMG_XPM:
    if ($supportedTypes & IMG_XPM) {
      $srcImage = imagecreatefromxpm($srcPath);
    } else {
      // XPM support not enabled on your PHP instance. Handle error here.
      exit('XPM support not enabled on this PHP instance');
    }
    break;
  default:
    // Unknown input file type. Handle error here. Currently prints some debugging info
    echo 'Unknown input file type';
    var_dump($info);
    exit;
}

// Create the destination image resource
$dstImage = imagecreatetruecolor($dstWidth, $dstHeight);

// Resample source image onto destination image
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $dstWidth + 1, $dstHeight + 1, $srcWidth, $srcHeight);

// Save new image to disk
imagejpeg($dstImage, $dstPath);

// Destroy resources
imagedestroy($srcImage);
imagedestroy($dstImage);
$srcImage = $dstImage = NULL;

// Remove source image - are you sure you want to do this?
unlink($srcPath);

现在,这段代码似乎是为处理本地源图像而设计的,但您使用它的方式表明您可能会从用户那里获取 URL 并下载远程图像。如果是这种情况,请告诉我,我会稍微修改一下以更好地适应该目的。

于 2012-08-10T12:37:54.023 回答
0

变化的是,

$images_orig = ImageCreateFromJPEG($image); //for jpg

你可以在这里查看文档

您还可以查看ImageMagick 与 GD和PHP之间的一些比较: ImageMagick - Manual

希望这可以帮助

于 2012-08-12T13:55:33.310 回答
0

根据您想要的输出文件类型,添加并选择:

imagecreatefrompng 

 imagecreatefromgif 

 imagepng 

 imagegif
于 2012-08-10T12:20:40.193 回答