PHP上传图片并调整大小

发布于 2024-08-19 12:58:53 字数 174 浏览 4 评论 0原文

我正在编写一个使用 PHP 上传图片的脚本,我想让它在保存之前将图像大小调整为宽度 180。
我尝试使用 WideImage 库和 ->saveFileTO(...) 但当我在页面中包含 WideImage.php 时,页面变为空白!
所以这是我的脚本,如果您可以帮助我并告诉我如何使其保存调整大小的版本

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤云独去闲 2024-08-26 12:58:53

您可以使用 PHP GD 库 在上传时调整图像大小。

以下代码应该让您了解如何实现调整大小:

// 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);
}

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);
}
晒暮凉 2024-08-26 12:58:53

您可以使用我为此类任务编写的类:

http://code.google.com/p/image2/source/browse/#svn/trunk/includes/classes

<?php

    try
    {
        $image = new Image2($path_to_image);
    }
    catch (NotAnImageException $e)
    {
        printf("FILE PROVIDED IS NOT AN IMAGE, FILE PATH: %s", $path_to_image);
    }

    $image -> resize(array("width" => 180)) -> saveToFile($new_path); // be sure to exclude the extension
    $new_file_location = $image -> getFileLocation(); // this will include the extension for future use

You can use a class I've written for just such a task:

http://code.google.com/p/image2/source/browse/#svn/trunk/includes/classes

<?php

    try
    {
        $image = new Image2($path_to_image);
    }
    catch (NotAnImageException $e)
    {
        printf("FILE PROVIDED IS NOT AN IMAGE, FILE PATH: %s", $path_to_image);
    }

    $image -> resize(array("width" => 180)) -> saveToFile($new_path); // be sure to exclude the extension
    $new_file_location = $image -> getFileLocation(); // this will include the extension for future use
追星践月 2024-08-26 12:58:53

您甚至不需要使用 WideImage 库。

在这里检查这个脚本:
http://bgallz.org/502/php-upload-resize-image/

首先上传图像并保存到临时图像文件。该脚本运行一个带有最大高度或最大宽度输入的表单。因此,它将根据新的宽度/高度生成一个新的图像文件,然后将临时图像复制到服务器上创建的新图像上。

您可以通过以下代码看到这一点:

// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight);
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);

You don't even need to use the WideImage library.

Check this script here:
http://bgallz.org/502/php-upload-resize-image/

You start by uploading the image and saving to a temp image file. This script runs off a form with inputs for the max height or max width. So it will then generate a new image file based on the new width/height and then copy the temp image onto the new one created on the server.

You see this with the following code:

// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight);
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
拔了角的鹿 2024-08-26 12:58:53

不要使用任何库
检查这个脚本
http://dr-wordpress.blogspot.com/2013 /12/image-resizing-using-php.html
刚刚给出了(0-99)的图像质量
此代码将在上传时自动调整图像大小

Dont use any library
Check this script
http://dr-wordpress.blogspot.com/2013/12/image-resizing-using-php.html
Just gave the quality of imges from (0-99)
this code will automatically resize the images while uploading

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文