0

这是我的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。

4

1 回答 1

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);
} 
于 2013-02-12T06:28:32.803 回答