将Image resize脚本展开为.PNG和.GIF

时间:2012-08-10 12:05:13

标签: php image resize gif

我有一个脚本可以完美地调整.JPG图像的大小,它确实完成了它应该做的事情。 但是,我想为用户提供添加.PNG en .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);

3 个答案:

答案 0 :(得分:0)

根据所需的输出文件类型,添加并选择:

imagecreatefrompng 

 imagecreatefromgif 

 imagepng 

 imagegif

答案 1 :(得分: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并下载远程图像。如果是这种情况,请告诉我,我会稍微修改一下以更好地适应这个目的。

答案 2 :(得分:0)

改变是什么,

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

您可以查看 here 文档

您还可以浏览 Some comparisons between ImageMagick and GD PHP: ImageMagick - Manual

希望这有帮助

相关问题