使用 gd 裁剪图像然后上传到数据库但不调整大小

发布于 2024-07-27 15:58:53 字数 1950 浏览 2 评论 0原文

我的代码基本上应该将图像裁剪为 219px x 127px 并将图像保存到数据库中,但我遇到错误并且无法弄清楚。

<?php
if(isset($_POST['btnupload']) && $_FILES['imglogo']['size'] > 0) {


$tmpname = $_FILES['imglogo']['tmp_name'];
$imgsize = $security->secure($_FILES['imglogo']['size']);
$imgtype = $security->secure($_FILES['imglogo']['type']);
$school = $security->secure($_POST['school']);

//crop image
$canvas = imagecreatetruecolor(219,127);
$imgattrib = getimagesize($tmpname);
$source = imagecreatefromjpeg($tmpname);
imagecopyresized($canvas,$source,0,0,0,0,219,127,$imgattrib[0],$imgattrib[1]);

$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = addslashes($content);
fclose($handle);




$save = mysql_query("insert into tbl_school_preview  values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());
//header("Location: school-catalog.php?page=school_preview");
}
?>

我没有看到任何错误,但作物没有发生。 难道我有什么问题吗?

[更新的代码] 这是由于某种原因拇指无法保存的新块。

if(isset($_POST['btnupload']) && $_FILES['imglogo']['size'] > 0) {

//$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $_FILES['imglogo']['tmp_name'];
$imgsize = $security->secure($_FILES['imglogo']['size']);
$imgtype = $security->secure($_FILES['imglogo']['type']);
$school = $security->secure($_POST['school']);

//crop image
$canvas = imagecreatetruecolor(219,127);
$imgattrib = getimagesize($tmpname);
$source = imagecreatefromjpeg($tmpname);
imagecopyresized($canvas,$source,0,0,0,0,219,127,$imgattrib[0],$imgattrib[1]);



$newfile = imagejpeg($canvas,'thumb.jpg',100);

$handle = fopen($newtmpfile, "r");
$content = fread($newtmpfile, filesize($newtmpfile));
$content = addslashes($content);
fclose($handle);




$save = mysql_query("insert into tbl_school_preview values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());

My code basically should crop the image to 219px by 127px and save the image to the database but I'm getting errors and can't figure it out.

<?php
if(isset($_POST['btnupload']) && $_FILES['imglogo']['size'] > 0) {


$tmpname = $_FILES['imglogo']['tmp_name'];
$imgsize = $security->secure($_FILES['imglogo']['size']);
$imgtype = $security->secure($_FILES['imglogo']['type']);
$school = $security->secure($_POST['school']);

//crop image
$canvas = imagecreatetruecolor(219,127);
$imgattrib = getimagesize($tmpname);
$source = imagecreatefromjpeg($tmpname);
imagecopyresized($canvas,$source,0,0,0,0,219,127,$imgattrib[0],$imgattrib[1]);

$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = addslashes($content);
fclose($handle);




$save = mysql_query("insert into tbl_school_preview  values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());
//header("Location: school-catalog.php?page=school_preview");
}
?>

I don't see any errors but the crop isn't happening. Do I have something wrong?

[Updated code]
Here is the new piece of block for some reason the thumb won't save.

if(isset($_POST['btnupload']) && $_FILES['imglogo']['size'] > 0) {

//$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $_FILES['imglogo']['tmp_name'];
$imgsize = $security->secure($_FILES['imglogo']['size']);
$imgtype = $security->secure($_FILES['imglogo']['type']);
$school = $security->secure($_POST['school']);

//crop image
$canvas = imagecreatetruecolor(219,127);
$imgattrib = getimagesize($tmpname);
$source = imagecreatefromjpeg($tmpname);
imagecopyresized($canvas,$source,0,0,0,0,219,127,$imgattrib[0],$imgattrib[1]);



$newfile = imagejpeg($canvas,'thumb.jpg',100);

$handle = fopen($newtmpfile, "r");
$content = fread($newtmpfile, filesize($newtmpfile));
$content = addslashes($content);
fclose($handle);




$save = mysql_query("insert into tbl_school_preview values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());

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

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

发布评论

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

评论(3

总以为 2024-08-03 15:58:53

您不需要执行任何 fopen / fwrite 操作来保存图像。 将所有内容替换为:

$filename = '/path/to/desired/save/location.png'
imagepng($canvas, $filename);

这样您就可以开始了。 当您给 imagepng 函数提供第二个参数时,它将写入文件。 imagegif 和 imagejpeg 也是如此。

You don't need to do any fopen / fwrite stuff to save your image. Replace all that with something like:

$filename = '/path/to/desired/save/location.png'
imagepng($canvas, $filename);

And you should be good to go. The imagepng function will write the file when you give it the second parameter. Same would be true for imagegif and imagejpeg.

野却迷人 2024-08-03 15:58:53

您应该保存 $canvas 的内容,而不是原始图像的内容。

使用 imagepng 输出到临时文件,或者直接输出流,即然后你将不得不拦截。

ob_start();

imagepng($canvas);

$out = ob_get_contents();

ob_end_clean();

You should save the content of $canvas, not the content of the original image.

Output it to temporary file with imagepng, or directly output stream, which you will then have to intercept.

ob_start();

imagepng($canvas);

$out = ob_get_contents();

ob_end_clean();

瀟灑尐姊 2024-08-03 15:58:53

只有两个提示:

首先,我不会将文件保存到文件系统中。您可以使用ob_start() 获取文件内容。

ob_start();
imagepng($canvas);
$imageString = ob_get_contents();
ob_end_clean();

$save = mysql_query("
    insert into tbl_school_preview    
    values(null,'$school',$imagestring,'$imgtype','$imgsize')
    ")or die(mysql_error());

还可以使用 imagecopyresampled() 获得更好的缩略图质量。 插入数据库时​​,请注意图像代码中的奇怪字符。

Just two tips:

I would first of all not to save the file to the filesystem.. You can use ob_start() to get the content of the file.

ob_start();
imagepng($canvas);
$imageString = ob_get_contents();
ob_end_clean();

$save = mysql_query("
    insert into tbl_school_preview    
    values(null,'$school',$imagestring,'$imgtype','$imgsize')
    ")or die(mysql_error());

Also use imagecopyresampled() for better quality of the thumbnail. Beware of strange characters in the image code when inserting into the database.

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