图像裁剪系统不适用于png或gif,但适用于jpg

时间:2013-02-12 06:24:49

标签: php

这是我的php代码:

<?php

$dst_x = 0;
$dst_y = 0;
$src_x = $_POST['left'];
$src_y = $_POST['top'];
$dst_w = $_POST['width'];
$dst_h = $_POST['height'];
$src_w = $_POST['width'];
$src_h = $_POST['height'];

$file_name = 'test.gif';

$allowed = array('jpg','jpeg','gif','png');
$file_extension= explode('.', $file_name); 
$file_extn= strtolower(end($file_extension));

$dst_image = imagecreatetruecolor($dst_w,$dst_h);

if(in_array($file_extn, $allowed) == 'jpg, jpeg'){
    $src_image = imagecreatefromjpeg($file_name);
} 
else if(in_array($file_extn, $allowed) == 'gif') {
    $src_image = imagecreatefromgif($file_name);
}
else if(in_array($file_extn, $allowed) == 'png') {
    $src_image = imagecreatefrompng($file_name);
}

imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagejpeg($dst_image, "new.jpg");

?>

当我裁剪图像时,它适用于jpg但不适用于png或gif。它显示的只是一张空白图片。我确定if语句有问题。这里有什么问题?如果我单独做,没有if语句,它就没有问题,是的,我希望我的输出是jpeg。

1 个答案:

答案 0 :(得分:0)

首先,你在这里犯了一个错误。

else if(in_array($file_extn, $allowed) == 'png') {
   $src_image = imagecreatefromgif($file_name);
}

应该是:

else if(in_array($file_extn, $allowed) == 'png') {
   $src_image = imagecreatefrompng($file_name);
}

并且必须有单独的if语句。

if(in_array($file_extn, $allowed) == 'jpg, jpeg'){
   $src_image = imagecreatefromjpeg($file_name);
}