Meda
Meda

Reputation: 65

how to resize the uploading image and moving to a folder

i am trying to re size the uploading image and moving to a folder, i try with the blow code, but it is not work. i created a function for re sizing the photo, calling that function from another function, but image is not re sized, and photo is not moved to folder.

$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;

}

Upvotes: 1

Views: 601

Answers (2)

Behzad Hassani
Behzad Hassani

Reputation: 2139

For resizing an image in php you can use Imagick::resizeImage from this article or use this article

Of course we can use Gd library which has low overhead by recommendation of @CD001. You can find explanation of GD re sizing in this article

Edit for More Explanation

for using Imagick::resizeImage you should see this description

here is the prototype of the function:

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

Parameters

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.

If you want to use gd library here is simple source code

<?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);

Upvotes: 0

Yagnik Detroja
Yagnik Detroja

Reputation: 929

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);
 }

This your function which is put in your function file

that function has make Folder in the thumbnails in the image folder when you call the function file.

Following way to call the function when image display.

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

Here the first is path of the image and 122:means width and 81:height and True/false True:crop the image and false: only resize the image.

And define the Uploadroot in your config file this path for the image folder.

Hope this works.

Thanks.

Upvotes: 1

Related Questions