大家好,我有一个上传一个或多个图片的网站文件上传页面。
问题是每当我上传文件时我的图像都会创建拇指,因此它应该创建
上传其显示确认标志但未创建
的图像数量的大拇指图片大拇指。
实际上我已从PHP学院
下载包包含此文件(create.thumb.php),我已经调用了该函数及其上传文本
图像成功但没有在Thumbs文件夹中创建图像的拇指这里是代码:
**create.thumb.php**
<?php
function create_thumb($directory, $image, $destination) {
$image_file = $image;
$image = $directory.$image;
if(file_exists($image)){
$source_size = getimagesize($image);
if($source_size !== false){
$thumb_width = (int)$width;
$thumb_height = (int)$height;
switch($source_size["mime"]){
case "image/jpeg" : $source = imagecreatefromjpeg($image); break;
case "image/png" : $source = imagecreatefrompng($image); break;
case "image/gif" : $source = imagecreatefromgif($image); break;
} $source_aspect = round(($source_size[0] / $source_size[1]), 1);
$thumb_aspect = round(($thumb_width / $thumb_height), 1);
if($source_aspect < $thumb_aspect){
$new_size = array($thumb_width, ($thumb_width / $source_size[0]) * $source_size[1]);
$source_pos = array(0, ($new_size[1] - $thumb_height) / 2);
} elseif($source_aspect > $thumb_aspect){
$new_size = array(($thumb_width / $source_size[1]) * $source_size[0], $thumb_height);
$source_pos = array(($new_size[0] - $thumb_width) / 2, 0);
} else {
$new_size = array($thumb_width, $thumb_height);
$source_pos = array(0, 0);
} if($new_size[0] < 1){
$new_size[0] = 1;
} if($new_size[1] < 1){
$new_size[1] = 1;
} $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $source, 0, 0, $source_pos[0], $source_pos[1], $new_size[0], $new_size[1], $source_size[0], $source_size[1]);
switch($source_size["mime"]){
case "image/jpeg" : imagejpeg($thumb, $destination.$image_file); break;
case "image/png" : imagepng($thumb, $destination.$image_file); break;
case "image/gif" : imagegif($thumb, $destination.$image_file); break;
}
}
}
}
?>
-------------------------------------------------------------------------------------------------
**index.php**
<?php
require_once('db.php');
require_once('create.thumb.php');
@$PropertyName=$_POST['pname'];
@$PropertyStatus=$_POST['pstatus'];
@$PropertyID=$_POST['propertyid'];
if(isset($_FILES['file_upload']) && !empty($_FILES['file_upload']))
{
$propertyquery="INSERT INTO properties(PropertyID, PropertyName, PropertyStatus)
VALUES('$PropertyID', '$PropertyName', '$PropertyStatus')";
$propertyqueryrun=$connection->query($propertyquery);
$id = $connection->insert_id;
if(!$propertyqueryrun)
{
echo '<br><br> Property Insertion Failed';
}
else
{
echo '<br><br> The Property Information Insertion was Successfully <br><br>';
}
mkdir("upload/$PropertyID");
$files=$_FILES['file_upload'];
for($x = 0; $x < count($files['name']); $x++)
{
$name=$files['name'][$x];
$tmp_name=$files['tmp_name'][$x];
if(move_uploaded_file($tmp_name, "upload/$PropertyID/".$name))
{
$insert_id=$id;
**create_thumb('upload/$PropertyID/',$name,'thumbs/$PropertyID/');**
$imagequery="INSERT INTO propertyimages(PropertyImageID, ImageName, ImagePath) VALUES('$insert_id', '$name', 'upload/$PropertyID/$name')";
$imagequeryrun=$connection->query($imagequery);
echo 'Image '. $name .' Uploaded Successfully <br>';
}
else
{
echo 'YOU HAVE TO SELECT ONE PIC AT LEAST';
}
}
}
?>
答案 0 :(得分:0)
查看下面的网址,它将帮助您获得更好的拇指图像功能代码
http://webcheatsheet.com/php/create_thumbnail_images.php
OR
http://davidwalsh.name/create-image-thumbnail-php
OR
http://phpthumb.sourceforge.net/
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>
答案 1 :(得分:0)
简单的risize脚本,试试这个:
<?php
$html = PHP_EOL;
if (!empty($_FILES['images'])) {
$finfo = new finfo(FILEINFO_MIME);
for ($i=0;;$i++) {
switch (true) {
case (!isset($_FILES['images']['tmp_name'][$i])):
break 2;
case (!is_uploaded_file($filename = $_FILES['images']['tmp_name'][$i])):
case (($type = $finfo->file($filename)) === false):
continue 2;
case ($type === 'image/png; charset=binary'):
$img = imagecreatefrompng($filename);
break;
case ($type === 'image/jpeg; charset=binary'):
$img = imagecreatefromjpeg($filename);
break;
case ($type === 'image/gif; charset=binary'):
$img = imagecreatefromgif($filename);
break;
default:
continue 2;
}
list($width, $height) = getimagesize($filename);
$new_width = 100;
$new_height = (int)($new_width * $height / $width);
$new_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled(
$new_img, $img,
0, 0, 0, 0,
$new_width, $new_height, $width, $height
);
switch (true) {
case ($type === 'image/png; charset=binary'):
imagepng($new_img, $filename);
break;
case ($type === 'image/jpeg; charset=binary'):
imagejpeg($new_img, $filename);
break;
default:
imagegif($new_img, $filename);
}
$new_filename = './tmp/'.basename($filename);
if (move_uploaded_file($filename,$new_filename))
$html .= sprintf('<p><img src="%s" /></p>'.PHP_EOL, $new_filename);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Resizer</title>
<style>
label { display: block; }
</style>
</head>
<body>
<fieldset>
<legend>Select Image File (PNG, JPEG, GIF available)</legend>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<label><input type="file" name="images[]" /></label>
<label><input type="file" name="images[]" /></label>
<label><input type="file" name="images[]" /></label>
<label><input type="submit" value="Resize!" /></label>
</form>
</fieldset>
<fieldset>
<legend>Resized Images</legend><?php
echo $html;
?>
</fieldset>
</body>
</html>
请随意重新制作。