Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

problem when trying to rename uploaded images using session value

Guest
Feb 03, 2010 Feb 03, 2010

Hi,

I have a form where 9 images are uploaded made into thumb nail size and then stored in a file. What I´m trying to do is rename those thumbnails from their original name to username_0, username_1, username_2 etc for each of the 9 thumbs. The username comes from the registration form on the same page. I have created a session variable for the username and am trying to use that to change to name of the image name, whilst my images are uploading and resizing the name stays as the original and not as the new username_$number. I have pasted relevant code below and would very much appreciate any help with this:

<?php session_start();

...................

// check that form has been submitted and that name is not empty and has no errors
if ($_POST && !empty($_POST['directusername'])) {
// set session variable
$_SESSION['directusername'] = $_POST['directusername'];
}
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 5120000); 
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'J:/xampp/htdocs/propertypages/uploads/');
// convert the maximum size to KB
$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');
foreach ($_FILES['photo']['name'] as $number => $file) {
// replace any spaces in the filename with underscores
$file = str_replace(' ', '_', $file);
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['photo']['size'] [$number] > 0 && $_FILES['photo']['size'] [$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['photo']['type'] [$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['photo']['error'] [$number]) {
case 0:
include('Includes/create_thumbs.inc.php');
break;
case 3:
$result[] = "Error uploading $file. Please try again.";
default:
$result[] = "System error uploading $file. Please contact us for further assistance.";
}
}
elseif ($_FILES['photo']['error'] [$number] == 4) {
$result[] = 'No file selected';
}
else {
$result[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
}
................CODE BELOW IS THE INCLUDES FILE THAT MAKES AND TRIES TO RENAME THE THUMBS................................................

<?php
// define constants
define('THUMBS_DIR', 'J:/xampp/htdocs/propertypages/uploads/thumbs/');
define('MAX_WIDTH_THB', 75);
define('MAX_HEIGHT_THB', 75);
set_time_limit(600);
// process the uploaded image
if (is_uploaded_file($_FILES['photo']['tmp_name'][$number] )) {
$original = $_FILES['photo']['tmp_name'][$number] ;
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// check that original image is big enough
if ($width < 270 || $height < 270) {
$result[] = 'Image dimensions are too small, a minimum image of 270 by 270 is required';
}
else { // crop image to a square before resizing to thumb
if ($width > $height) {
$x = ceil(($width - $height) / 2);
$width = $height;
$y = 0;
}
else if($height > $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
$x = 0;
}
// calculate the scaling ratio
if ($width <= MAX_WIDTH_THB && $height <= MAX_HEIGHT_THB) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH_THB/$width;
}
else {
$ratio = MAX_HEIGHT_THB/$height;
}
if (isset($_SESSION['directusername'])) {
$username = $_SESSION['directusername'];
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['photo']['name'][$number]));
// change the images names to the user name _ the photo number
$newname = str_replace ('name', '$username_$number', $name);
// 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 uploading image, please try again or contact us for further assistance';
}
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, $x, $y, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
if (function_exists('imagegif'))  {
$success[] = imagegif($thumb, THUMBS_DIR.$newname.'.gif');
$photoname = $newname.'.gif';
}
else {
$success[] = imagejpeg($thumb, THUMBS_DIR.$newname.'.jpg',50);
$photoname = $newname.'.jpg';
}
break;
case 2:
$success[] = imagejpeg($thumb, THUMBS_DIR.$newname.'.jpg', 100);
$photoname = $newname.'.jpg';
break;
case 3:
$success[] = imagepng($thumb, THUMBS_DIR.$newname.'.png');
$photoname = $newname.'.png';
}
if ($success) {
$result[] = "Upload sucessful";
}
else {
$result[] = 'Problem uploading image, please try again or contact us for further assistance';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}
}
}
?>

..................................................

I hope i´ve supplied enough information and look forward to receiving any help or advise in this matter.

TOPICS
Server side applications
392
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 05, 2010 Feb 05, 2010

Use double quotes here:

$newname = str_replace ('name', '$username_$number', $name);

Change it to this:

$newname = str_replace ('name', "$username_$number", $name);
Translate
LEGEND ,
Feb 05, 2010 Feb 05, 2010

Use double quotes here:

$newname = str_replace ('name', '$username_$number', $name);

Change it to this:

$newname = str_replace ('name', "$username_$number", $name);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 05, 2010 Feb 05, 2010
LATEST

Thank you so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines