简体   繁体   English

PHP上传和调整图像大小

[英]PHP upload and resize image

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. 我正在编写一个使用PHP上传图片的脚本,我想让它在保存之前将图片大小调整为180。
I tried using the WideImage library and ->saveFileTO(...) but when I include the WideImage.php in the page, the page goes blank !! 我尝试使用WideImage库和 - > saveFileTO(...)但是当我在页面中包含WideImage.php时,页面变为空白!!
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. 您可以使用PHP GD库在上载时调整图像大小。

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

You don't even need to use the WideImage library. 您甚至不需要使用WideImage库。

Check this script here: http://bgallz.org/502/php-upload-resize-image/ 请在此处查看此脚本: 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);

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

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM