上载php imagecreatefromjpeg时显示为黑色的图像
这个代码直到最近才开始工作,就在我即将参加面试的时候,但是代码不再做它应该做的事情 页面获取数据库中描述的图像列表,由用户上传,然后使用文件名从目录中收集图像 上传时,用户只需浏览硬盘并点击表单上的上传按钮。将图像加载到预加载图像/目录,并将副本大小调整为缩略图大小,然后将其放置在另一个目录preload images thumbs/中,然后页面会向管理员发送邮件以批准该照片 这一切都很好,但我唯一的工作记录是在2016年9月。现在它只创建黑色图像,尽管大小调整正确,但没有图像 我在这里和其他网站上检查了一些类似的问题,但似乎没有任何效果。我甚至与主机联系,以在php.ini中增加内存 这里有一些代码,如果有人能帮忙的话上载php imagecreatefromjpeg时显示为黑色的图像,php,image,Php,Image,这个代码直到最近才开始工作,就在我即将参加面试的时候,但是代码不再做它应该做的事情 页面获取数据库中描述的图像列表,由用户上传,然后使用文件名从目录中收集图像 上传时,用户只需浏览硬盘并点击表单上的上传按钮。将图像加载到预加载图像/目录,并将副本大小调整为缩略图大小,然后将其放置在另一个目录preload images thumbs/中,然后页面会向管理员发送邮件以批准该照片 这一切都很好,但我唯一的工作记录是在2016年9月。现在它只创建黑色图像,尽管大小调整正确,但没有图像 我在这里和其他网
<?php
function make_thumb($src,$dest,$desired_width, $desired_height, $ext) {
/* read the source image */
if ( $ext == 'jpg' || ext == 'jpeg') {
$source_image = imagecreatefromjpeg($src);
}
if ( $ext == 'png') {
$source_image = imagecreatefrompng($src);
}
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
if ( $ext == 'jpg' || ext == 'jpeg') {
imagejpeg($virtual_image,$dest);
}
if ( $ext == 'png') {
imagepng($virtual_image,$dest);
}
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
/* copy source image at a resized size */
imagecopyresized($virtual_image,$source_image,300,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
}
/* function: returns files from dir */
function get_files($images_dir,$exts = array('jpg', 'jpeg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
/* function: returns a file's extension */
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
/** settings **/
$images_dir = 'preload-images/';
$thumbs_dir = 'preload-images-thumbs/';
$thumbs_width = 200;
$images_per_row = 4;
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width, $thumbs_height, $extension);
}
}
}
}
我尝试了您的代码,并修复了一些注意事项和imagecopyresized函数中的错误设置($dst_x)。。
$dst_x中的数字过高会使图像“超出范围”
php错误日志中是否记录了任何错误?函数make_thumb
有一个逻辑测试,根据文件扩展名确定要使用哪个例程生成新图像。然后继续使用$source\u image=imagecreatefromjpeg($src)代码>不考虑逻辑测试。然后,代码继续使用$virtual_image
,但这是由imagejpeg
或imagepng
调用后定义的。该php文件没有记录错误-该页面在2010年编写,直到今年早些时候才开始运行,但我在一次又一次的追踪之后,唯一剩下的证据就是2016年9月加载的一张图片完美!非常感谢!从昨天起,我就一直在琢磨这是怎么回事!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function make_thumb($src,$dest,$desired_width, $desired_height, $ext) {
/* read the source image */
if ( $ext == 'jpg' || ext == 'jpeg') {
$source_image = imagecreatefromjpeg($src);
}
if ( $ext == 'png') {
$source_image = imagecreatefrompng($src);
}
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height); # removed here because off notice.
if ( $ext == 'jpg' || ext == 'jpeg') {
imagejpeg($virtual_image,$dest);
}
if ( $ext == 'png') {
imagepng($virtual_image,$dest);
}
/* copy source image at a resized size */
#changed 300 into 0
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
}
/* function: returns files from dir */
function get_files($images_dir,$exts = array('jpg', 'jpeg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
/* function: returns a file's extension */
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
/** settings **/
$images_dir = 'preload-images/';
$thumbs_dir = 'preload-images-thumbs/';
$thumbs_width = 200;
$thumbs_height = null; # added because off notice..
$images_per_row = 4;
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width, $thumbs_height, $extension);
}
}
}
}
?>