根据我的评论,我意识到您可以从返回的数据中提取图像的类型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 并下载远程图像。如果是这种情况,请告诉我,我会稍微修改一下以更好地适应该目的。