好的,我已经有一段时间了,需要一些帮助。我有一个包含 3000 多张图片的目录,我需要缩略图,但我无法让 php 来做这件事:(
我从来没有用 php 制作缩略图,所以这对我来说是新的,我发现了一些脚本可以在您将图像上传到服务器时创建缩略图,我将从这里开始。
图像位置作为 URL 存储在 mysql 数据库中。我需要它来创建图像的缩略图,当然,还需要使用缩略图 url 更新数据库。
这是我用来整合我的需求的脚本:http:
//net.tutsplus.com/articles/news/how-to-dynamically-create-thumbnails/
我使用的是完全相同的functions.php & config .php 文件。
这是我得到的:
$row['image'] 是每个图像的位置,例如:http
://www.url.com/images/image.jpg
$filename 我正在剥离 url,所以它只提供 image.jpg
<?php
require 'config.php';
require 'functions.php';
require 'db.php';
$sql = mysql_query("SELECT * FROM pics");
while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
$filename = preg_replace('['.$url.']', '', $row['image']);
$source = $row['image'];
createThumbnail($filename);
$thumb_loc = $url . "images/thumbs/" . $filename;
$id = $row['id'];
mysql_query("UPDATE pics SET image_thumb = '$thumb_loc' WHERE id = '$id'");
}
?>
我为 $filename 提供了一个正确的文件名,为 $source 提供了图像源,所以它应该可以工作。
这是 functions.php 中的 createThumbnail 函数:
<?
function createThumbnail($filename) {
require 'config.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = $final_height_of_image;
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $filename);
$tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
$tn .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.';
echo $tn;
}
?>
以下是警告:
Warning: imagecreatefromjpeg(images/fullsized/image.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in functions.php on line 7
Warning: imagesx(): supplied argument is not a valid Image resource in functions.php on line 14
Warning: imagesy(): supplied argument is not a valid Image resource in functions.php on line 15
Warning: Division by zero in functions.php on line 18
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in functions.php on line 20
Warning: imagecopyresized(): supplied argument is not a valid Image resource in functions.php on line 22
Warning: imagejpeg(): supplied argument is not a valid Image resource in functions.php on line 30
有人可以帮我吗?