使用Verot Class.Upload.php 在上传时调整图片大小

3

我希望在上传图片时能够调整图片大小,我在网上找到了 verot class.upload.php。但是不幸的是,它对我没有用,无法上传也无法调整大小。所以我的问题是,这个API是否需要一些安装步骤?因为我只把 class.upload.php 上传到了我的服务器,并且我的函数是这样的:

public  function myImageR()
                     {


        $foo = new Upload($_FILES['img']); 
if ($foo->uploaded) {
   // save uploaded image with no changes
   $foo->Process('/public/images/aa/');
   if ($foo->processed) {
     $a = true;
   } else {
     $a = false;
   }
   // save uploaded image with a new name
   $foo->file_new_name_body = 'foo';
   $foo->Process('/public/images/aa/');
   if ($foo->processed) {
   $a = true;
   } else {
     $a = false;
   }   
   // save uploaded image with a new name,
   // resized to 100px wide
   $foo->file_new_name_body = 'image_resized';
   $foo->image_resize = true;
   $foo->image_convert = gif;
   $foo->image_x = 100;
   $foo->image_ratio_y = true;
   $foo->Process('/public/images/aa/');
   if ($foo->processed) {
    $a = true;
     $foo->Clean();
   } else {
    $a = false;
   } 
}   

                     }

但它根本不起作用,我正在使用XAMPP在本地主机上工作。


它说它需要GD库,所以您需要检查您的phpinfo()来确定GD是否已启用。 - Sagar Guhe
是的,它已经启用了,但仍然无法正常工作。 - Enigma
还要检查您的phpinfo()中的upload_max_filesizepost_max_size,这在某些情况下也可能会引起问题。同时,请检查错误日志以查看是否生成了任何错误信息。 - Sagar Guhe
1个回答

2

您需要安装GD库...例如,如果您使用的是Ubuntu:

sudo apt-get install php5-gd

然后重新启动Apache。

sudo /etc/init.d/apache2 restart

无论如何,我使用这个来调整图片大小:

上传:

   $uploaded_filename = '/www/html/picts/yourfoto.jpg';
   if(isset($_FILES['your_files_field_name'])){
        if (move_uploaded_file($_FILES['your_files_field_name']['tmp_name'], $uploaded_filename)) {
             chmod($uploaded_filename, 0755); 
             return $nome; // OK
         } else {
             return "ERROR"; 
         }
    }

调整大小:

<?php


    $w = 40; // set width
    $h = 40; // set height
    $path = './'; // path


$image_file = (string) filter_input(INPUT_GET, 'img');
$image_path = $path . $image_file;
$img = null;
$ext = @strtolower(end(explode('.', $image_path)));


if ($ext == 'jpeg') {
    $img = imagecreatefromjpeg($image_path);
} else if ($ext == 'jpg') {
    $img = imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
    $img = imagecreatefrompng($image_path);
} elseif ($ext == 'gif') {
    $img = imagecreatefromgif($image_path);
} elseif ($ext == 'bmp') {
    $img = imagecreatefromwbmp($image_path);
} else {
    exit("File type not found: " . $ext);
}


if ($img) {
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min($w / $width, $h / $height);

    if ($scale < 1) {
        $new_width = floor($scale * $width);
        $new_height = floor($scale * $height);
        $tmp_img = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }
}


if (!$img) {
    $img = imagecreate($w, $h);
    imagecolorallocate($img, 204, 204, 204);
    $c = imagecolorallocate($img, 153, 153, 153);
    $c1 = imagecolorallocate($img, 0, 0, 0);
    imageline($img, 0, 0, $w, $h, $c);
    imageline($img, $w, 0, 0, $h, $c);
    imagestring($img, 2, 12, 55, 'IMAGE ERROR', $c1);
}

header('Content-type: ' . 'image/png');
imagejpeg($img);

如果你想将调整大小后的文件保存到磁盘上:

$file = '/www/html/picts/yourfoto.jpg';
$save = imagejpeg($img, $file); // save the file

我希望你能够受益。

谢谢,艾伦。你知道像 HostGator 这样的 Web 托管服务器是否可用并安装了这个库吗?因为我想在那里托管我的网站。 - Enigma
我使用https://www.wiredtree.com/或任何VPS。如果我的答案解决了您的问题,我会很高兴如果您接受它。 - Allan Andrade

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接