使用 gd 裁剪图像然后上传到数据库但不调整大小
我的代码基本上应该将图像裁剪为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要执行任何 fopen / fwrite 操作来保存图像。 将所有内容替换为:
这样您就可以开始了。 当您给 imagepng 函数提供第二个参数时,它将写入文件。 imagegif 和 imagejpeg 也是如此。
You don't need to do any fopen / fwrite stuff to save your image. Replace all that with something like:
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.
您应该保存 $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();
只有两个提示:
首先,我不会将文件保存到文件系统中。您可以使用ob_start() 获取文件内容。
还可以使用 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.
Also use imagecopyresampled() for better quality of the thumbnail. Beware of strange characters in the image code when inserting into the database.