【问题标题】:Php Image upload failing and using imagecreatefromjpeg to process png and bmp files?PHP图像上传失败并使用imagecreatefromjpeg处理png和bmp文件?
【发布时间】:2014-10-30 01:21:12
【问题描述】:

您好,我有以下代码来创建图像的缩略图然后上传它。当我尝试上传下面附加的图片时,代码失败了,我不知道为什么。在此代码之前,有一个主图像上传,它始终有效,但上面的缩略图脚本大部分时间都有效,但由于某种原因不能附加图像。代码死了,因此页面输出主图像上传成功,但缩略图永远不会上传,页面的其余部分也不会。

此代码还会处理 jpeg 以外的图像吗?如果它不会,我将如何让它处理其他文件类型,如 bmp 和 png?

  // load image and get image size
  $img = imagecreatefromjpeg( $upload_path . $filename );
  $width = imagesx( $img );
  $height = imagesy( $img );

  // calculate thumbnail size
  $new_height = $thumbHeight;
  $new_width = floor( $width * ( $thumbHeight / $height ) );

  // create a new temporary image
  $tmp_img = imagecreatetruecolor( $new_width, $new_height );

  // copy and resize old image into new image 
  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

  // save thumbnail into a file
  imagejpeg( $tmp_img, $upload_paththumbs . $filename );

Image that fails

【问题讨论】:

  • 请解释一下“代码失败”是什么意思
  • 在此代码之前,有一个主图像上传始终有效,但上面的缩略图脚本大部分时间都有效,但由于某种原因不能附加图像。代码死了,因此页面输出主图像上传成功,但缩略图永远不会上传,页面的其余部分也不会。
  • 听起来您没有看到错误。使用error_reporting = E_ALLdisplay_errors = On 在您的php.ini 文件中启用正确的错误报告并重新启动您的网络服务器
  • “php.ini”文件已经按照你说的配置好了
  • 您能否检查您的 Web 服务器的错误日志文件中的错误?没有具体错误……那么代码就OK了,可能是参数不对,所以检查所有参数。

标签: php image file upload


【解决方案1】:

您的代码适用于您的图像,因此问题一定出在您的输入变量的设置值上。

正如 cmets 中已经建议的那样,检查您的 PHP 错误日志。如果这没有显示任何异常,则需要逐行调试代码。

对于您问题的第二部分:不,您的代码不适用于 JPEG 以外的图像。下面的更改将处理 GIF、JPEG 和 PNG。

请注意,函数exif_imagetype() 默认情况下可能不可用。在这种情况下,您需要在 PHP 配置中使用 activate the exif extension

$upload_path = './';
$filename = 'b4bAx.jpg';
$upload_paththumbs = './thumb-';
$thumbHeight = 50;

switch ( exif_imagetype( $upload_path . $filename ) ) {
    case IMAGETYPE_GIF:
        imagegif(
            thumb(imagecreatefromgif( $upload_path . $filename ), $thumbHeight),
            $upload_paththumbs . $filename
        );
        break;
    case IMAGETYPE_JPEG:
        imagejpeg(
            thumb(imagecreatefromjpeg( $upload_path . $filename ), $thumbHeight),
            $upload_paththumbs . $filename
        );
        break;
    case IMAGETYPE_PNG:
        imagepng(
            thumb(imagecreatefrompng( $upload_path . $filename ), $thumbHeight),
            $upload_paththumbs . $filename
        );
        break;
    default:
        echo 'Unsupported image type!';
}

function thumb($img, $thumbHeight) {
    // get image size
    $width = imagesx( $img );
    $height = imagesy( $img );

    // calculate thumbnail size
    $new_height = $thumbHeight;
    $new_width = floor( $width * ( $thumbHeight / $height ) );

    // create a new temporary image
    $tmp_img = imagecreatetruecolor( $new_width, $new_height );

    // copy and resize old image into new image
    imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

    // return thumbnail
    return $tmp_img;
}

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 2010-10-17
    • 2012-04-19
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2023-03-09
    相关资源
    最近更新 更多