how to move an image from folder and resize before saving to another folder ?

Hi There;

I am working on a project which multiple pages validation. My main issue is with the upload part of the code. What i am doing is when a user upload his images; i save them in a tempfolder then once form filled complete, i move the images from temp to mainfolder but before i want to resize the images before saving them in mainfolder.

How can i achieve that ?

For now i succeed doing the 1st part : save the img in temp folder then on next page i move to mainfolder.
Here is my code
imageuploaded.php
[php]<?php
define(‘MAX_FILE_SIZE’, 952070);
define(‘UPLOAD_DIR_TEMP’, BASE_URI.‘html/temp/’);
$max = number_format(MAX_FILE_SIZE/1024, 1).‘KB’;
// create an array of permitted MIME types
$permitted = array(‘image/gif’, ‘image/jpeg’, ‘image/pjpeg’,‘image/png’);

if (array_key_exists(‘upload’, $_POST)) {
foreach ($FILES[‘image’][‘name’] as $number => $file) {
// replace any spaces in the filename with underscores
$file = str_replace(’ ‘,’
’,$file);
$fileNom = pathinfo($_FILES[‘image’][“name”][$number], PATHINFO_FILENAME);
$ext = pathinfo($_FILES[‘image’][“name”][$number],PATHINFO_EXTENSION);
//$soruceimg = $_FILES[‘image’][‘tmp_name’];
//echo $file;
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES[‘image’][‘size’][$number] > 0 || $_FILES[‘image’][‘size’][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES[‘image’][‘type’][$number]) {
$typeOK = true;
break;
}
}

if ($sizeOK && $typeOK) {
switch($_FILES[‘image’][‘error’][$number]) {
case 0:

// get the date and time
ini_set(‘date.timezone’, ‘Africa/Abidjan’);
$now = date(‘Y-m-d-His’);
//$Nfilename = basename(substr($file,0,6).$now).".".$ext ." ";
$successes = move_uploaded_file($_FILES[‘image’][‘tmp_name’][$number], UPLOAD_DIR_TEMP.$file);
if ($successes) {
$_SESSION[‘imgname’]= $_FILES[‘image’][‘name’];
header(‘Location: test.php’);
//echo $file;
}
else {
$resultfile[] = “Error uploading $file. Please try again.”;
}
break;
case 3:
$resultfile[] = “Error uploading $file. Please try again.”;
default:
$resultfile[] = “System error uploading $file. Contact webmaster.”;
}
}
elseif ($_FILES[‘image’][‘error’][$number] == 4) {
$errNofile[] = 1;
}
else {
$resultfile[] = “$file cannot be uploaded. Maximum size: $max.Acceptable file types: gif, jpg, png.”;
}

}
}
[/php]

createthumbs.php
[php]session_sart();
$source = BASE_URI.‘html/temp/’;
define(‘THUMBS_DIR’, BASE_URI.‘html/products/thumbs/’);
define(‘MAX_WIDTH’, 200);
define(‘MAX_HEIGHT’, 200);
define(‘UPLOAD_DIR’, BASE_URI.‘html/products/’);
ini_set(‘date.timezone’, ‘Africa/Abidjan’);
$now = date(‘Y-m-d-His’);
if (isset($_SESSION[‘imgname’]) && is_array($_SESSION[‘imgname’]) ) {

foreach ($SESSION[‘imgname’] as $files) {
echo $files;
$img = str_replace(" ", "
", $files);
$ext = pathinfo($img,PATHINFO_EXTENSION);

if (file_exists($source.$img)) {
$ext = pathinfo($img,PATHINFO_EXTENSION);
// strip the extension off the image filename
$imagetypes = array(’/.gif$/’, ‘/.jpg$/’, ‘/.jpeg$/’, ‘/.png$/’);
$name = preg_replace($imagetypes, ‘’, basename(substr($img,0,6).$now));

list($width, $height, $type) = getimagesize($img);
if ($width != $height) {
echo “file width and height must be same”;
break;
}
elseif ( $width < 300 && $height < 300) {
echo “File must be at least 400 x 400”;
break;
}
else{
$ratio = 1;
$ratio = MAX_WIDTH/$width;
}

$Nfilename = $name.’.’.$ext;
$moved = rename($source.$img, UPLOAD_DIR.$Nfilename);

if (!$moved) {
echo “Problem moving file”.$img;
}
else{
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = ‘Cannot process GIF files. Please use JPEG or PNG.’;
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = ‘Cannot identify file type.’;
}

// make sure the image resource is OK
if (!$source) {
$result = ‘Problem copying original’;
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
if (function_exists(‘imagegif’)) {
$success = imagegif($thumb, THUMBS_DIR.$name.’_thb.gif’);
$thumb_name = $name.’_thb.gif’;
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.’_thb.jpg’, 50);
$thumb_name = $name.’_thb.jpg’;
}
break;
case 2:
$success = imagejpeg($thumb, THUMBS_DIR.$name.’_thb.jpg’, 100);
$thumb_name = $name.’_thb.jpg’;
break;
case 3:
$success = imagepng($thumb, THUMBS_DIR.$name.’_thb.png’);
$thumb_name = $name.’_thb.png’;
}
if ($success) {
$result = “$thumb_name created”;
}
else {
$result = ‘Problem creating thumbnail’;
}

if ($success) {
  echo $thumb_name;
}

// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}   

}

}
else{
echo “No file exist”;
}

}

}[/php]

The resize part does not work and i need someone to help me out

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service