直到最近才工作,就像我即将参加面试一样,但代码不再适用。
该页面,提取数据库中描述的图像列表,由用户上传,然后使用文件名从目录中收集图像。
上传时,用户只需浏览硬盘并点按表单上的上传按钮即可。图像被加载到preload-images /目录,副本被调整为缩略图大小并放在另一个目录preload-images-thumbs /中,然后页面邮寄管理员批准照片。
这一切都运行良好,但我唯一的工作记录是在2016年9月。现在它只创建黑色图像,虽然正确调整大小,但没有图像。
我在这里和其他网站上检查了一些类似的问题,但似乎没有任何效果。我甚至与主持人联系过php.ini
中的内存如果有人可以提供帮助,这里有一些代码......
<?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);
}
}
}
}
只需添加,所有旧图像,在2106年之前,都可以正常工作。我甚至尝试制作副本并重命名旧图像以尝试重新加载它们以查看它是否与图像格式有关,但这些仍处理黑盒子。
非常感谢任何帮助......
答案 0 :(得分:0)
我已尝试使用您的代码并在imagecopyresized函数中修复了一些注意事项和错误设置($ dst_x)。 对于$ dst_x中的高数字,图像会超出范围&#34;
<?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);
}
}
}
}
?>