gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:59 25 4
gpt4 key购买 nike

我想在上传时调整我在网上找到的图片 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;
}
}

}

但它根本不起作用我正在使用 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

希望对您有所帮助!

关于php - 使用 verot class.upload.php 在上传时调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32349576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com