无法创建拇指,图像为黑色 [英] Unable to create thumb, image is black

查看:61
本文介绍了无法创建拇指,图像为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从单个输入上传多个图像并在飞行中创建所有上传图像的拇指形式但当我运行代码时我只得到黑色图像但原始图像与上传相同

 <?php 

$ newname = md5(rand()* time());
$ file1 = isset($ _ FILES ['files'] ['name'] [0])? $ _FILES ['files'] ['name'] [0]:null;
$ file2 = isset($ _ FILES ['files'] ['name'] [1])? $ _FILES ['files'] ['name'] [1]:null;
$ file3 = isset($ _ FILES ['files'] ['name'] [2])? $ _FILES ['files'] ['name'] [2]:null;
$ file4 = isset($ _ FILES ['files'] ['name'] [3])? $ _FILES ['files'] ['name'] [3]:null;
$ file5 = isset($ _ FILES ['files'] ['name'] [4])? $ _FILES ['files'] ['name'] [4]:null;
if(isset($ _ FILES ['files'])){
$ errors = array();
foreach($ _FILES ['files'] ['tmp_name'] as $ key => $ tmp_name){
$ file_name = $ key。 $ _FILES [文件] [名] [$关键]
$ file_size = $ _FILES ['files'] ['size'] [$ key];
$ file_tmp = $ _FILES ['files'] ['tmp_name'] [$ key];
$ file_type = $ _FILES ['files'] ['type'] [$ key];
if($ file_size> 2097152000){
$ errors [] ='文件大小必须小于2 MB';
}
$ desired_dir =user_data /;
if(empty($ errors)== true){
if(is_dir($ desired_dir)== false){
mkdir($ desired_dir,0700); //创建目录(如果它不存在)
}
if(is_dir($ desired_dir /。$ file_name)== false){
move_uploaded_file($ file_tmp,$ desired_dir / 。$ newname。$ file_name);
} else {//如果存在另一个文件,则重命名该文件
$ new_dir =$ desired_dir /。 $ newname。 $ FILE_NAME;
重命名($ file_tmp,$ new_dir);
}
} else {
print_r($ errors);
}
}
if(空($ error)){
echoFILE:$ file1< br>;
echoFILE:$ file2< br>;
echoFILE:$ file3< br>;
echoFILE:$ file4< br>;
echoFILE:$ file5< br>;
}
}
$ orig_directory =$ desired_dir; //完整图像文件夹
$ thumb_directory =thumb /; //缩略图文件夹

/ *打开缩略图目录并循环显示所有拇指:* /
$ dir_handle = @opendir($ orig_directory); //打开完整图像dirrectory
if($ dir_handle> 1){//检查以确保文件夹已打开

$ allowed_types = array('jpg','jpeg',' GIF, 'PNG');
$ file_type = array();
$ ext ='';
$ title ='';
$ i = 0;

while($ file_name = @readir($ dir_handle)){
/ *跳过系统文件:* /
if($ file_name =='。'|| $ file_name =='..')继续;

$ file_type = explode('。',$ file_name); //这将获取图像的文件名
$ ext = strtolower(array_pop($ file_type));

/ *使用文件名(不带扩展名)作为图像标题:* /
$ title = implode('。',$ file_type);
$ title = htmlspecialchars($ title);

/ *如果允许文件扩展名:* /
if(in_array($ ext,$ allowed_types)){

/ *如果你想将图像输入数据库,在这里做你的mysql查询* /

/ *这里的代码是教程开头的代码* /
/ *输出每个图像:* /

$ nw = 100;
$ nh = 100;
$ source =$ desired_dir {$ file_name};
$ stype = explode(。,$ source);
$ stype = $ stype [count($ stype)-1];
$ dest =thumb / {$ file_name};

$ size = getimagesize($ source);
$ w = $ size [0];
$ h = $ size [1];

switch($ stype){
case'gif':
$ simg = imagecreatefromgif($ source);
休息;
case'jpg':
$ simg = imagecreatefromjpeg($ source);
休息;
case'png':
$ simg = imagecreatefrompng($ source);
休息;
}

$ dimg = imagecreatetruecolor($ nw,$ nh);
$ wm = $ w / $ nw;
$ hm = $ h / $ nw;
$ h_height = $ nh / 2;
$ w_height = $ nw / 2;

if($ w> $ h){
$ adjusted_width = $ w / $ hm;
$ half_width = $ adjusted_width / 2;
$ int_width = $ w / $ hm;
imagecopyresampled($ dimg,$ simg, - $ int_width,0,0,0,$ adjusted_width,$ nh,$ w,$ h);
} else {
imagecopyresampled($ dimg,$ simg,0,0,0,0,$ nw,$ nh,$ w,$ h);
}
imagejpeg($ dimg,$ dest,100);
}
}

/ *关闭目录* /
@closedir($ dir_handle);

}
?>

当我运行代码这个怎么出来放文件,不知道发生什么事可以一些一个帮我找到错误
为所有类型的图像格式创建黑拇指



当我从上面的代码中删除以下代码时它起作用这个代码做了什么

  if($ w> $ h){
$ adjusted_width = $ w / $ hm;
$ half_width = $ adjusted_width / 2;
$ int_width = $ w / $ hm;
imagecopyresampled($ dimg,$ simg, - $ int_width,0,0,0,0,$ adjusted_width,$ nw,$ nh,$ w,$ h);
}其他


解决方案

问题



问题位于以下行:

  imagecopyresampled($ dimg,$ simg, -  $ int_width,0,0,0,$ adjusted_width,$ nh,$ w,$ h); 

为什么使用负值作为目的地的x?您的源图片实际上位于目标图片的左侧,因此您的目标图片显示为空。



解决方案



<我邀请您使用以下函数调整图像大小:

  function resizePreservingAspectRatio($ img,$ targetWidth,$ targetHeight )
{
$ srcWidth = imagesx($ img);
$ srcHeight = imagesy($ img);

//确定新的宽度/高度保持宽高比
$ srcRatio = $ srcWidth / $ srcHeight;
$ targetRatio = $ targetWidth / $ targetHeight;
if(($ srcWidth< = $ targetWidth)&&($ srcHeight< = $ targetHeight))
{
$ imgTargetWidth = $ srcWidth;
$ imgTargetHeight = $ srcHeight;
}
else if($ targetRatio> $ srcRatio)
{
$ imgTargetWidth =(int)($ targetHeight * $ srcRatio);
$ imgTargetHeight = $ targetHeight;
}
其他
{
$ imgTargetWidth = $ targetWidth;
$ imgTargetHeight =(int)($ targetWidth / $ srcRatio);
}

//创建所需大小的新图像
$ targetImg = imagecreatetruecolor($ targetWidth,$ targetHeight);

//如果缩小的图像不适合新尺寸,则添加透明度
$ targetTransparent = imagecolorallocate($ targetImg,255,0,255);
imagefill($ targetImg,0,0,$ targetTransparent);
imagecolortransparent($ targetImg,$ targetTransparent);

//复制图像,以新图像为中心(如果它不适合它)
$ imageImg,$ img,($ targetWidth - $ imgTargetWidth) / 2,//居中
($ targetHeight - $ imgTargetHeight)/ 2,//居中
0,0,$ imgTargetWidth,$ imgTargetHeight,$ srcWidth,$ srcHeight
);

返回$ targetImg;
}



实施



 <?php 
$ newname = md5(rand()* time());
$ file1 = isset($ _ FILES ['files'] ['name'] [0])? $ _FILES ['files'] ['name'] [0]:null;
$ file2 = isset($ _ FILES ['files'] ['name'] [1])? $ _FILES ['files'] ['name'] [1]:null;
$ file3 = isset($ _ FILES ['files'] ['name'] [2])? $ _FILES ['files'] ['name'] [2]:null;
$ file4 = isset($ _ FILES ['files'] ['name'] [3])? $ _FILES ['files'] ['name'] [3]:null;
$ file5 = isset($ _ FILES ['files'] ['name'] [4])? $ _FILES ['files'] ['name'] [4]:null;
if(isset($ _ FILES ['files']))
{
$ errors = array();
foreach($ _FILES ['files'] ['tmp_name'] as $ key => $ tmp_name)
{
$ file_name = $ key。 $ _FILES [文件] [名] [$关键]
$ file_size = $ _FILES ['files'] ['size'] [$ key];
$ file_tmp = $ _FILES ['files'] ['tmp_name'] [$ key];
$ file_type = $ _FILES ['files'] ['type'] [$ key];
if($ file_size> 2097152000)
{
$ errors [] ='文件大小必须小于2 MB';
}
$ desired_dir =user_data /;
if(empty($ errors)== true)
{
if(is_dir($ desired_dir)== false)
{
mkdir($ desired_dir ,0700); //创建目录(如果它不存在)
}
if(is_dir($ desired_dir /。$ file_name)== false)
{
move_uploaded_file($ file_tmp, $ desired_dir /。$ newname。$ file_name);
}
else
{//如果存在另一个文件,则重命名该文件
$ new_dir =$ desired_dir /。 $ newname。 $ FILE_NAME;
重命名($ file_tmp,$ new_dir);
}
}
其他
{
print_r($ errors);
}
}
if(空($ error))
{
echoFILE:$ file1< br>;
echoFILE:$ file2< br>;
echoFILE:$ file3< br>;
echoFILE:$ file4< br>;
echoFILE:$ file5< br>;
}
$ orig_directory =$ desired_dir; //完整图像文件夹
$ thumb_directory =thumb /; //缩略图文件夹

/ *打开缩略图目录并循环显示所有拇指:* /
$ dir_handle = @opendir($ orig_directory); //打开完整图片dirrectory
if($ dir_handle> 1)
{//检查以确保文件夹已打开
$ allowed_types = array('jpg','jpeg',' gif','png');
$ file_type = array();
$ ext ='';
$ title ='';
$ i = 0;

while($ file_name = @readir($ dir_handle))
{
/ *跳过系统文件:* /
if($ file_name =='。 '|| $ file_name =='..')
继续;

$ file_type = explode('。',$ file_name); //这将获取图像的文件名
$ ext = strtolower(array_pop($ file_type));

/ *使用文件名(不带扩展名)作为图像标题:* /
$ title = implode('。',$ file_type);
$ title = htmlspecialchars($ title);

/ *如果允许文件扩展名:* /
if(in_array($ ext,$ allowed_types))
{

/ * If你想把图像输入数据库,在这里做你的mysql查询* /

/ *这里的代码是教程开头的代码* /
/ *输出每个image:* /

$ nw = 100;
$ nh = 100;
$ source =$ desired_dir {$ file_name};
$ stype = explode(。,$ source);
$ stype = $ stype [count($ stype) - 1];
$ dest =thumb / {$ file_name};

$ size = getimagesize($ source);
$ w = $ size [0];
$ h = $ size [1];

开关($ stype)
{
case'gif':
$ simg = imagecreatefromgif($ source);
休息;
case'jpg':
$ simg = imagecreatefromjpeg($ source);
休息;
case'png':
$ simg = imagecreatefrompng($ source);
休息;
}

$ dimg = resizePreservingAspectRatio($ simg,$ nw,$ nh);
imagepng($ dimg,$ dest);
}
}

/ *关闭目录* /
@closedir($ dir_handle);
}
}

函数resizePreservingAspectRatio($ img,$ targetWidth,$ targetHeight)
{
$ srcWidth = imagesx($ img);
$ srcHeight = imagesy($ img);

//确定新的宽度/高度保持宽高比
$ srcRatio = $ srcWidth / $ srcHeight;
$ targetRatio = $ targetWidth / $ targetHeight;
if(($ srcWidth< = $ targetWidth)&&($ srcHeight< = $ targetHeight))
{
$ imgTargetWidth = $ srcWidth;
$ imgTargetHeight = $ srcHeight;
}
else if($ targetRatio> $ srcRatio)
{
$ imgTargetWidth =(int)($ targetHeight * $ srcRatio);
$ imgTargetHeight = $ targetHeight;
}
其他
{
$ imgTargetWidth = $ targetWidth;
$ imgTargetHeight =(int)($ targetWidth / $ srcRatio);
}

//创建所需大小的新图像
$ targetImg = imagecreatetruecolor($ targetWidth,$ targetHeight);

//如果缩小的图像不适合新尺寸,则添加透明度
$ targetTransparent = imagecolorallocate($ targetImg,255,0,255);
imagefill($ targetImg,0,0,$ targetTransparent);
imagecolortransparent($ targetImg,$ targetTransparent);

//复制图像,以新图像为中心(如果它不适合它)
$ imageImg,$ img,($ targetWidth - $ imgTargetWidth) / 2,//居中
($ targetHeight - $ imgTargetHeight)/ 2,//居中
0,0,$ imgTargetWidth,$ imgTargetHeight,$ srcWidth,$ srcHeight
);

返回$ targetImg;
}

?>

< form method =postenctype =multipart / form-data>
< input name =files []type =file/>< br />
< input name =files []type =file/>< br />
< input name =files []type =file/>< br />
< input name =files []type =file/>< br />
< input name =files []type =file/>< br />
< input type =submit/>
< / form>

注意:我保存为PNG,就像你想要使用非方形图像的100x100图像一样(例如800x600),并且在不超出其宽高比的情况下,我们应该将透明度放在未使用的空间后面,JPEG不支持它。


Am uploading Multiple image from single input and create thumb form all uploaded image on fly But when i run code i get only black image but orginal image is same as uploaded

    <?php

$newname = md5(rand() * time());
$file1 = isset($_FILES['files']['name'][0]) ? $_FILES['files']['name'][0] : null;
$file2 = isset($_FILES['files']['name'][1]) ? $_FILES['files']['name'][1] : null;
$file3 = isset($_FILES['files']['name'][2]) ? $_FILES['files']['name'][2] : null;
$file4 = isset($_FILES['files']['name'][3]) ? $_FILES['files']['name'][3] : null;
$file5 = isset($_FILES['files']['name'][4]) ? $_FILES['files']['name'][4] : null;
if (isset($_FILES['files'])) {
    $errors = array();
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
        $file_name = $key . $_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];
        if ($file_size > 2097152000) {
            $errors[] = 'File size must be less than 2 MB';
        }
        $desired_dir = "user_data/";
        if (empty($errors) == true) {
            if (is_dir($desired_dir) == false) {
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if (is_dir("$desired_dir/" . $file_name) == false) {
                move_uploaded_file($file_tmp, "$desired_dir/" . $newname . $file_name);
            } else {                                  // rename the file if another one exist
                $new_dir = "$desired_dir/" . $newname . $file_name;
                rename($file_tmp, $new_dir);
            }
        } else {
            print_r($errors);
        }
    }
    if (empty($error)) {
        echo "FILE : $file1<br>";
        echo "FILE : $file2<br>";
        echo "FILE : $file3<br>";
        echo "FILE : $file4<br>";
        echo "FILE : $file5<br>";
    }
}
$orig_directory = "$desired_dir";    //Full image folder
$thumb_directory =  "thumb/";    //Thumbnail folder

/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = @opendir($orig_directory); //Open Full image dirrectory
if ($dir_handle > 1){ //Check to make sure the folder opened

$allowed_types=array('jpg','jpeg','gif','png');
$file_type=array();
$ext='';
$title='';
$i=0;

while ($file_name = @readdir($dir_handle)) {
    /* Skipping the system files: */
    if($file_name=='.' || $file_name == '..') continue;

    $file_type = explode('.',$file_name);    //This gets the file name of the images
    $ext = strtolower(array_pop($file_type));

    /* Using the file name (withouth the extension) as a image title: */
    $title = implode('.',$file_type);
    $title = htmlspecialchars($title);

    /* If the file extension is allowed: */
    if(in_array($ext,$allowed_types)) {

        /* If you would like to inpute images into a database, do your mysql query here */

        /* The code past here is the code at the start of the tutorial */
        /* Outputting each image: */

        $nw = 100;
        $nh = 100;
        $source = "$desired_dir{$file_name}";
        $stype = explode(".", $source);
        $stype = $stype[count($stype)-1];
        $dest = "thumb/{$file_name}";

        $size = getimagesize($source);
        $w = $size[0];
        $h = $size[1];

        switch($stype) {
            case 'gif':
                $simg = imagecreatefromgif($source);
                break;
            case 'jpg': 
                $simg = imagecreatefromjpeg($source);
                break;
            case 'png':
                $simg = imagecreatefrompng($source);
                break;
        }

        $dimg = imagecreatetruecolor($nw, $nh);
        $wm = $w/$nw;
        $hm = $h/$nw;
        $h_height = $nh/2;
        $w_height = $nw/2;

        if($w> $h) {
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $w / $hm;
            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
        } else {
            imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }
            imagejpeg($dimg,$dest,100);
        }
}

/* Closing the directory */
@closedir($dir_handle);

}
?>

When i run code this how am getting out put file, don't know whats going on can some one help me find the error Black thumb is created for all type image formate

When i remove the following code from above code it works what does this code does

if($w> $h) {
        $adjusted_width = $w / $hm;
        $half_width = $adjusted_width / 2;
        $int_width = $w / $hm;
        imagecopyresampled($dimg,$simg,-$int_width,0,0,0,0,$adjusted_width,$nw,$nh,$w,$h);
    } else 

解决方案

Problem

The problem is located in the following line:

imagecopyresampled($dimg, $simg, -$int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h);

Why are you using a negative value as destination's x? Your source image is actually put at the left of your target image, so your target image appears empty.

Solution

I invite you to use the following function to resize your image:

function resizePreservingAspectRatio($img, $targetWidth, $targetHeight)
{
   $srcWidth = imagesx($img);
   $srcHeight = imagesy($img);

   // Determine new width / height preserving aspect ratio
   $srcRatio = $srcWidth / $srcHeight;
   $targetRatio = $targetWidth / $targetHeight;
   if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight))
   {
      $imgTargetWidth = $srcWidth;
      $imgTargetHeight = $srcHeight;
   }
   else if ($targetRatio > $srcRatio)
   {
      $imgTargetWidth = (int) ($targetHeight * $srcRatio);
      $imgTargetHeight = $targetHeight;
   }
   else
   {
      $imgTargetWidth = $targetWidth;
      $imgTargetHeight = (int) ($targetWidth / $srcRatio);
   }

   // Creating new image with desired size
   $targetImg = imagecreatetruecolor($targetWidth, $targetHeight);

   // Add transparency if your reduced image does not fit with the new size
   $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
   imagefill($targetImg, 0, 0, $targetTransparent);
   imagecolortransparent($targetImg, $targetTransparent);

   // Copies image, centered to the new one (if it does not fit to it)
   imagecopyresampled(
      $targetImg, $img, ($targetWidth - $imgTargetWidth) / 2, // centered
      ($targetHeight - $imgTargetHeight) / 2, // centered
      0, 0, $imgTargetWidth, $imgTargetHeight, $srcWidth, $srcHeight
   );

   return $targetImg;
}

Implementation

<?php
$newname = md5(rand() * time());
$file1 = isset($_FILES['files']['name'][0]) ? $_FILES['files']['name'][0] : null;
$file2 = isset($_FILES['files']['name'][1]) ? $_FILES['files']['name'][1] : null;
$file3 = isset($_FILES['files']['name'][2]) ? $_FILES['files']['name'][2] : null;
$file4 = isset($_FILES['files']['name'][3]) ? $_FILES['files']['name'][3] : null;
$file5 = isset($_FILES['files']['name'][4]) ? $_FILES['files']['name'][4] : null;
if (isset($_FILES['files']))
{
   $errors = array ();
   foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name)
   {
      $file_name = $key . $_FILES['files']['name'][$key];
      $file_size = $_FILES['files']['size'][$key];
      $file_tmp = $_FILES['files']['tmp_name'][$key];
      $file_type = $_FILES['files']['type'][$key];
      if ($file_size > 2097152000)
      {
         $errors[] = 'File size must be less than 2 MB';
      }
      $desired_dir = "user_data/";
      if (empty($errors) == true)
      {
         if (is_dir($desired_dir) == false)
         {
            mkdir("$desired_dir", 0700);        // Create directory if it does not exist
         }
         if (is_dir("$desired_dir/" . $file_name) == false)
         {
            move_uploaded_file($file_tmp, "$desired_dir/" . $newname . $file_name);
         }
         else
         {                                  // rename the file if another one exist
            $new_dir = "$desired_dir/" . $newname . $file_name;
            rename($file_tmp, $new_dir);
         }
      }
      else
      {
         print_r($errors);
      }
   }
   if (empty($error))
   {
      echo "FILE : $file1<br>";
      echo "FILE : $file2<br>";
      echo "FILE : $file3<br>";
      echo "FILE : $file4<br>";
      echo "FILE : $file5<br>";
   }
   $orig_directory = "$desired_dir";    //Full image folder
   $thumb_directory = "thumb/";    //Thumbnail folder

   /* Opening the thumbnail directory and looping through all the thumbs: */
   $dir_handle = @opendir($orig_directory); //Open Full image dirrectory
   if ($dir_handle > 1)
   { //Check to make sure the folder opened
      $allowed_types = array ('jpg', 'jpeg', 'gif', 'png');
      $file_type = array ();
      $ext = '';
      $title = '';
      $i = 0;

      while ($file_name = @readdir($dir_handle))
      {
         /* Skipping the system files: */
         if ($file_name == '.' || $file_name == '..')
            continue;

         $file_type = explode('.', $file_name);    //This gets the file name of the images
         $ext = strtolower(array_pop($file_type));

         /* Using the file name (withouth the extension) as a image title: */
         $title = implode('.', $file_type);
         $title = htmlspecialchars($title);

         /* If the file extension is allowed: */
         if (in_array($ext, $allowed_types))
         {

            /* If you would like to inpute images into a database, do your mysql query here */

            /* The code past here is the code at the start of the tutorial */
            /* Outputting each image: */

            $nw = 100;
            $nh = 100;
            $source = "$desired_dir{$file_name}";
            $stype = explode(".", $source);
            $stype = $stype[count($stype) - 1];
            $dest = "thumb/{$file_name}";

            $size = getimagesize($source);
            $w = $size[0];
            $h = $size[1];

            switch ($stype)
            {
               case 'gif':
                  $simg = imagecreatefromgif($source);
                  break;
               case 'jpg':
                  $simg = imagecreatefromjpeg($source);
                  break;
               case 'png':
                  $simg = imagecreatefrompng($source);
                  break;
            }

            $dimg = resizePreservingAspectRatio($simg, $nw, $nh);
            imagepng($dimg, $dest);
         }
      }

      /* Closing the directory */
      @closedir($dir_handle);
   }
}

function resizePreservingAspectRatio($img, $targetWidth, $targetHeight)
{
   $srcWidth = imagesx($img);
   $srcHeight = imagesy($img);

   // Determine new width / height preserving aspect ratio
   $srcRatio = $srcWidth / $srcHeight;
   $targetRatio = $targetWidth / $targetHeight;
   if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight))
   {
      $imgTargetWidth = $srcWidth;
      $imgTargetHeight = $srcHeight;
   }
   else if ($targetRatio > $srcRatio)
   {
      $imgTargetWidth = (int) ($targetHeight * $srcRatio);
      $imgTargetHeight = $targetHeight;
   }
   else
   {
      $imgTargetWidth = $targetWidth;
      $imgTargetHeight = (int) ($targetWidth / $srcRatio);
   }

   // Creating new image with desired size
   $targetImg = imagecreatetruecolor($targetWidth, $targetHeight);

   // Add transparency if your reduced image does not fit with the new size
   $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
   imagefill($targetImg, 0, 0, $targetTransparent);
   imagecolortransparent($targetImg, $targetTransparent);

   // Copies image, centered to the new one (if it does not fit to it)
   imagecopyresampled(
      $targetImg, $img, ($targetWidth - $imgTargetWidth) / 2, // centered
      ($targetHeight - $imgTargetHeight) / 2, // centered
      0, 0, $imgTargetWidth, $imgTargetHeight, $srcWidth, $srcHeight
   );

   return $targetImg;
}

?>

<form method="post" enctype="multipart/form-data">
   <input name="files[]" type="file"/><br/>
   <input name="files[]" type="file"/><br/>
   <input name="files[]" type="file"/><br/>
   <input name="files[]" type="file"/><br/>
   <input name="files[]" type="file"/><br/>
   <input type="submit"/>
</form>

Note: I am saving as PNG, as if you want a 100x100 image using a non-square image (such as 800x600), and without breaking its aspect ratio, we should put transparency behind the unused space and JPEG do not support it.

这篇关于无法创建拇指,图像为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆