在上传时使用verot class.upload.php调整图像大小


resize image on upload with verot class.upload.php

我想在上传时调整我的图像大小,我在web上找到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;
   } 
}   
                     }

但是如果它不工作,我正在工作的localhost与xammp

您需要安装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

我希望这有帮助!