Php imagecreatefromjpeg显示空白图像
我在html中使用以下代码调用php文件来创建缩略图并将其显示在此页面上 &w=150&h=&00“alt=”Image“/> minimal.php的代码如下:Php imagecreatefromjpeg显示空白图像,php,thumbnails,Php,Thumbnails,我在html中使用以下代码调用php文件来创建缩略图并将其显示在此页面上 &w=150&h=&00“alt=”Image“/> minimal.php的代码如下: <?php function redimensionner_image($chemin_image, $largeur_max, $hauteur_max) { list($src_w, $src_h) = getimagesize($chemin_image); $dst_w = $largeur
<?php
function redimensionner_image($chemin_image, $largeur_max, $hauteur_max)
{
list($src_w, $src_h) = getimagesize($chemin_image);
$dst_w = $largeur_max;
$dst_h = $hauteur_max;
if($src_w < $dst_w)
$dst_w = $src_w;
// Teste les dimensions tenant dans la zone
$test_h = round(($dst_w / $src_w) * $src_h);
$test_w = round(($dst_h / $src_h) * $src_w);
if(!$dst_h)// Si Height final non précisé (0)
$dst_h = $test_h;
elseif(!$dst_w) // Sinon si Width final non précisé (0)
$dst_w = $test_w;
elseif($test_h>$dst_h) // Sinon teste quel redimensionnement tient dans la zone
$dst_w = $test_w;
else
$dst_h = $test_h;
$array_ext = explode('.', $chemin_image);
$extension = strtolower($array_ext[count($array_ext)-1]);
if($extension == 'jpg' || $extension == 'jpeg')
$img_in = imagecreatefromjpeg($chemin_image);
else if($extension == 'png')
$img_in = imagecreatefrompng($chemin_image);
else if($extension == 'gif')
$img_in = imagecreatefromgif($chemin_image);
else
return false;
$img_out = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $dst_w, $dst_h, imagesx($img_in), imagesy($img_in));
imagejpeg($img_out);
}
?>
但是,Imagecreatefromjpeg在调整大小后返回黑色图像。请提供任何帮助,首先您不需要分解文件扩展名,因为
getimagesize
将为您提供以下类型:
list($width, $height, $type) = getimagesize($source);
接下来,我不按照您的尺寸计算,尝试简化它们,f.e.:
$scale = min($maxWidth / $width, $maxHeight / $height, 1); // We only use downsampling, no upsampling! If you need upsampling remove the '1' parameter
$newWidth = min($width * $scale, $maxWidth);
$newHeight = min($height * $scale, $maxHeight);
简单的重缩放并保留纵横比
以下是我在项目中使用的代码供参考:
function SaveImageAsJpeg($sourceFilename, $destFilename, $maxWidth = 0, $maxHeight = 0, $jpegQuality = 80) {
list($width, $height, $type) = getimagesize($sourceFilename);
$sourceImage = false;
switch ($type) {
case IMAGETYPE_GIF:
$sourceImage = imagecreatefromgif($sourceFilename);
break;
case IMAGETYPE_JPEG:
$sourceImage = imagecreatefromjpeg($sourceFilename);
break;
case IMAGETYPE_PNG:
$sourceImage = imagecreatefrompng($sourceFilename);
break;
}
if (!$sourceImage)
return false;
if (($maxWidth == 0) || ($maxHeight == 0)) {
// Don't resize
$destinationImage = imagecreatetruecolor($width, $height);
imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height);
imagejpeg($destinationImage, $destFilename, $jpegQuality);
imagedestroy($destinationImage);
} else {
// Resize image
$scale = min($maxWidth / $width, $maxHeight / $height, 1); // We only use downsampling, no upsampling! If you need upsampling remove the '1' parameter
$newWidth = min($width * $scale, $maxWidth);
$newHeight = min($height * $scale, $maxHeight);
$destinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($destinationImage, $destFilename, $jpegQuality);
imagedestroy($destinationImage);
}
imagedestroy($sourceImage);
return true;
}
当然,上面的代码不会返回图像数据。它只是将重新缩放的图像保存到服务器上的其他文件中。但是,如果您将NULL
作为$destFilename
参数传递,它会将图像数据输出到输出流:
header('Content-Type: image/jpeg');
SaveImageAsJpeg($sourceFilename, NULL, 200, 200);
如果你仍然得到一个黑色图像,我建议增加PHP内存限制。如果你可以修改
PHP.INI
,调整memory\u limit
设置。否则使用.htaccess
文件和这一行,例如:PHP\u value memory\u limit 64M
我不知道,什么是redimmensionner\u image()
应该是,但是Imagecreatefromjpeg()是
返回一个资源,您不能只打印到浏览器。调用Imagecreatefromjpeg
的位置在哪里,是否可以向我们显示redimensionner\u image
的代码?redimensionner image意味着调整图像大小我想您的代码不完整,请告诉我们错误在哪里,以便更好地了解其他问题。抱歉,语言错误我刚刚更新了代码…仍然没有显示任何内容