PHP上传和调整图像大小 [英] PHP upload and resize image

查看:147
本文介绍了PHP上传和调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP上传一张图片的脚本,我想在保存图片之前将图片的大小调整为180.

我试过使用WideImage库和 - > saveFileTO(。 ..),但是当我在页面中包含WideImage.php时,页面变为空白!

所以这里是我的脚本,如果你能帮助我,告诉我如何让它保存调整大小的版本
php.net/manual/en/book.image.phprel =nofollow noreferrer> PHP的GD库来调整上传的图像。



下面的代码应该给你一个如何实现调整大小的想法:

  //从图片获取图片信息
$ image_info = getimagesize($ photo);
$ width = $ new_width = $ image_info [0];
$ height = $ new_height = $ image_info [1];
$ type = $ image_info [2];

//载入图片
switch($ type)
{
case IMAGETYPE_JPEG:
$ image = imagecreatefromjpeg($ photo);
break;
case IMAGETYPE_GIF:
$ image = imagecreatefromgif($ photo);
break;
case IMAGETYPE_PNG:
$ image = imagecreatefrompng($ photo);
break;
default:
die('Error loading'。$ photo。' - File type'。$ type。'not supported');
}

//创建一个新的,调整大小的图像
$ new_width = 180;
$ new_height = $ height /($ width / $ new_width);
$ new_image = imagecreatetruecolor($ new_width,$ new_height);
imagecopyresampled($ new_image,$ image,0,0,0,0,$ new_width,$ new_height,$ width,$ height);

//将新图片保存在原始照片的顶部
switch($ type)
{
case IMAGETYPE_JPEG:
imagejpeg($ new_image ,$ photo,100);
break;
case IMAGETYPE_GIF:
imagegif($ new_image,$ photo);
break;
case IMAGETYPE_PNG:
imagepng($ new_image,$ photo);
break;
默认值:
die('Error image:'。$ photo);
}


I am working on a script that uploads a picture using PHP and I wanna make it resize the image to width 180 before saving it.
I tried using the WideImage library and ->saveFileTO(...) but when I include the WideImage.php in the page, the page goes blank !!
So here is my script if you can help me and tell me how to make it save the resized version

解决方案

You can use the PHP GD library to resize an image on upload.

The following code should give you an idea of how to implement the resize:

// Get the image info from the photo
$image_info = getimagesize($photo);
$width = $new_width = $image_info[0];
$height = $new_height = $image_info[1];
$type = $image_info[2];

// Load the image
switch ($type)
{
    case IMAGETYPE_JPEG:
        $image = imagecreatefromjpeg($photo);
        break;
    case IMAGETYPE_GIF:
        $image = imagecreatefromgif($photo);
        break;
    case IMAGETYPE_PNG:
        $image = imagecreatefrompng($photo);
        break;
    default:
        die('Error loading '.$photo.' - File type '.$type.' not supported');
}

// Create a new, resized image
$new_width = 180;
$new_height = $height / ($width / $new_width);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Save the new image over the top of the original photo
switch ($type)
{
    case IMAGETYPE_JPEG:
        imagejpeg($new_image, $photo, 100);
        break;
    case IMAGETYPE_GIF:
        imagegif($new_image, $photo);         
        break;
    case IMAGETYPE_PNG:
        imagepng($new_image, $photo);
        break;
    default:
        die('Error saving image: '.$photo);
}

这篇关于PHP上传和调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆