php - 创建图像缩略图不起作用

标签 php mysql

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

问题是每当我上传文件时都会为我的图像创建拇指,因此它应该创建

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

对图像表示赞赏。

实际上我已经从 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';
         }

    }

}
?>

最佳答案

查看下面的网址,它将帮助您获得更好的 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);
?>

关于php - 创建图像缩略图不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16536464/

相关文章:

php - 两个并发连接/请求锁

php - 如何找到/路径/到/artisan ?

php - register_shutdown_function 没有被调用

php - Content-disposition:inline header 不会显示内联图像?

mysql - 检查 MySQL 事务中的错误

php - 负日期间隔

php - 用于生成智能 URL 的搜索解决方案

mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column in 'field list' using Criteria and mapping annotation

javascript - 尝试创建一个 AngularJS 函数来删除 MySQL 条目

mysql - 提供正确的 sequelize.js 对象连接到数据库( Multi-Tenancy 应用程序)