0

大家好,我的网站有一个文件上传页面,可以上传一张或多张图片。这

问题是每当我上传文件时为我的图像创建拇指所以它应该创建

上传显示确认按摩但未创建的图像数量的拇指

图片的拇指。

实际上我已经从 PHP 学院下载了一个包

包含此文件 (create.thumb.php) 只是我调用了该函数及其上传文本

图像成功但未在 Thumbs 文件夹中创建图像的 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';
         }

    }

}
?>
4

2 回答 2

0

查看下面的 url,它将帮助您获得更好的 Thumb 图像功能代码

http://webcheatsheet.com/php/create_thumbnail_images.php

或者

http://davidwalsh.name/create-image-thumbnail-php

或者

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);
?>
于 2013-05-14T06:30:19.313 回答
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>

请随时重新制作它。

于 2013-05-14T06:33:02.000 回答