如何调整上传图像的大小并移动到文件夹


how to resize the uploading image and moving to a folder

我正在尝试重新调整上传图像的大小并移动到一个文件夹,我尝试使用blow代码,但它不起作用。我创建了一个重新调整照片大小的函数,从另一个函数调用该函数,但图像没有重新调整大小,照片也没有移动到文件夹中。

$final_save_dir = 'techpic';
$thumbname = $_FILES['tpic']['name'];
$imgName = $final_save_dir . '/' . $_FILES['tpic']['name'];
if(@move_uploaded_file($_FILES['tpic']['tmp_name'], $final_save_dir . '/' . $_FILES['tpic']['name']))
{   
$this->createThumbnail($thumbname,"600","600",$final_save_dir,$final_save_dir);
}
function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir)

{$path=$uploadDir.'/'$image_name;

  $mime = getimagesize($path);
  if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); }
  if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); }
  if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); }
  if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); }
  $old_x          =   imageSX($src_img);
  $old_y          =   imageSY($src_img);
  if($old_x > $old_y) 
  {
      $thumb_w    =   $new_width;
      $thumb_h    =   $old_y*($new_height/$old_x);
  }
  if($old_x < $old_y) 
  {
      $thumb_w    =   $old_x*($new_width/$old_y);
      $thumb_h    =   $new_height;
  }
  if($old_x == $old_y) 
  {
      $thumb_w    =   $new_width;
      $thumb_h    =   $new_height;
  }
  $dst_img        =   ImageCreateTrueColor($thumb_w,$thumb_h);
  imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

  // New save location
  $new_thumb_loc = $moveToDir . $image_name;
  if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,8); }
  if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
  if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
  if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
  imagedestroy($dst_img); 
  imagedestroy($src_img);
  return $result;

}

function img($field = 'image', $width = null, $height = null, $crop = false, $alt = null, $turl = null) {
global $editormode;
$val = $field;
if (!$val)
    $val = 'no-image.png';

$alt = ($alt) ? $alt : stem(basename($val));
if ($width == null && $height == null)
    $imgf = get_dir() . $val;
else
    $imgf = gen_img($val, $width, $height, $crop);
if (!$imgf)
    return "";
$url = $imgf;
if (!$turl)
    return "<img src='$url' alt='$alt'/>'n";
else
    return "<a href='$turl'><img src='$url' alt='$alt'/></a>";
  }
function get_dir() {
return  "upload/";
}
function gen_img($fileval, $width, $height, $crop) {
if (!$fileval)
    return null;
$fname = get_dir() . $fileval;
if (!is_readable($fname))
    return null;
$stem = stem(basename($fname));
if ($width != null && $height != null) {
    $sz = getimagesize($fname);
    if ($sz[0] == $width && $sz[1] == $height) {
        return substr($fname, strlen(UPLOADROOT));
    }
    $sep = ($crop) ? '__' : '_';
    $outname = thumb_dir($fname) . $stem . $sep . $width . "x" . $height . "." . suffix($fname);
    if (!is_readable($outname) || filemtime($outname) < filemtime($fname))
        createthumb($fname, $outname, $width, $height, $crop);
}
else if ($width != null && $height == null) {
    $outname = thumb_dir($fname) . $stem . "_" . $width . "." . suffix($fname);
    if (!is_readable($outname) || filemtime($outname) < filemtime($fname))
        createthumb($fname, $outname, $width, $crop);
} else
    $outname = $fname;
//echo $outname; die();
return $outname;
}
function thumb_dir($path) {
$enddir = strrpos($path, "/");
$dir = substr($path, 0, $enddir) . "/.thumbnails/";
if (!file_exists($dir))
    mkdir($dir, 0777, true);
return $dir;
}
function createthumb($source, $dest, $new_w, $new_h = null, $crop = false) {
if (!file_exists($source))
    return null;
$src_img = 0;
$src_img = image_create($source);
$old_w = imageSX($src_img);
$old_h = imageSY($src_img);
$x = $y = 0;
if ($new_h == null) { // we want a square thumb, cropped if necessary
    if ($old_w > $old_h) {
        $x = ceil(($old_w - $old_h) / 2);
        $old_w = $old_h;
    } else if ($old_h > $old_w) {
        $y = ceil(($old_h - $old_w) / 2);
        $old_h = $old_w;
    }
    $thumb_w = $thumb_h = $new_w;
} else if ($crop) {
    $thumb_w = $new_w;
    $thumb_h = $new_h;
    $oar = $old_w / $old_h;
    $nar = $new_w / $new_h;
    if ($oar < $nar) {
        $y = ($old_h - $old_h * $oar / $nar) / 2;
        $old_h = ($old_h * $oar / $nar);
    } else {
        $x = ($old_w - $old_w * $nar / $oar) / 2;
        $old_w = ($old_w * $nar / $oar);
    }
} else if ($new_w * $old_h / $old_w <= $new_h) { // retain aspect ratio, limit by new_w
    $thumb_h = $new_w * $old_h / $old_w;
    $thumb_w = $new_w;
} else { // retain aspect ratio, limit by new_h
    $thumb_w = $new_h * $old_w / $old_h;
    $thumb_h = $new_h;
}
$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
imagecolortransparent($dst_img, imagecolorallocatealpha($dst_img, 0, 0, 0, 127));
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
imagecopyresampled($dst_img, $src_img, 0, 0, $x, $y, $thumb_w, $thumb_h, $old_w, $old_h);
image_save($dst_img, $dest);
imagedestroy($dst_img);
imagedestroy($src_img);
}
function image_create($source) {
$suf = strtolower(suffix($source));
if ($source == '.jpg')
    mylog("wtf", "source: $source", true);
if ($suf == "png")
    return imagecreatefrompng($source);
else if ($suf == "jpg" || $suf == "jpeg")
    return imagecreatefromjpeg($source);
else if ($suf == "gif")
    return imagecreatefromgif($source);
return null;
 }
 function image_save($dst_img, $dest) {
$suf = strtolower(suffix($dest));
if ($suf == "png")
    imagepng($dst_img, $dest);
else if ($suf == "jpg" || $suf == "jpeg")
    imagejpeg($dst_img, $dest);
else if ($suf == "gif")
    imagegif($dst_img, $dest);
 }

这是你的功能,放在你的功能文件中

当您调用函数文件时,该函数会在图像文件夹中的缩略图中创建文件夹。

以下方式调用图像显示时的函数。

 <?php echo img('pages/sample_image.jpg', 122, 81, TRUE) ?>

这里第一个是图像的路径,122:表示宽度,81:表示高度,True/false True:裁剪图像,false:仅调整图像大小。

并在配置文件中为映像文件夹定义Uploadroot路径。

希望这能奏效。

谢谢。

为了在php中调整图像的大小,您可以使用本文中的Imagick::resizeImage或使用本文的

当然,通过@CD001的推荐,我们可以使用开销较低的Gd库。你可以在本文中找到GD重新调整大小的解释

编辑以获取更多解释

对于使用Imagick::resizeImage,您应该看到以下描述

这是函数的原型:

bool Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = false ] )

参数

columns
Width of the image
rows
Height of the image
filter
Refer to the list of filter constants.
blur
The blur factor where > 1 is blurry, < 1 is sharp.
bestfit
Optional fit parameter.

如果你想使用gd库,这里是简单的源代码

<?php
// File and new size
//the original image has 800x600
$filename = 'images/picture.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output and free memory
//the resized image will be 400x300
imagejpeg($thumb);
imagedestroy($thumb);