Forum Moderators: coopster

Message Too Old, No Replies

Renaming image files and reduce quality help needed

rename image files

         

zorro

10:22 pm on Apr 24, 2010 (gmt 0)

10+ Year Member



Any help greatly appreciated,

Currently we are having problems with certain foreign character in image file names being uploaded
to our server for example (ä) and (ñ) often seen of Spanish keyboards we have only just come across this as we now offer our service to Spain.
Not only have we noticed this problems we have also noticed that files ending in JPG (Capital letters) are not being resized using the separate resize function.

The programmers we used to create the script have been great over the years but are no longer operational.
I have looked for days on the internet for help and tried many different methods albeit unsuccessful.

At present files are held with a temp_name and renamed - id-filename.jpg (id being the id of the users property) the id is added to prevent a duplicate filename being used.

Image uploaded are resized using a separate function (see below) but the files ending in Capital JPG don't seem to resize properly.

Ideally we would like to rename any images uploaded for example:
userimage.jpg
USERIMAGE.JPG
USER_IMAGE.JPEG
invalidcharacter.jpg
anythingelse.jpeg

To a file name like (id)(hyphen)(date/time)(dot)(lowercase jpg)
For example: 78-220420102215.jpg
(78 being the property id number, hyphen, 22ndApril2010 at ten fifteen, lowercase jpg)

Just can't seem to get it right!

Also we would like to reduce the orginal quality slightly to reduce the file size!


HERE IS THE CODE ON THE PAGE WHERE USER UPLOADS THE IMAGE:
-----------------------------------------------------------------------------------------------
FIRST PART OF CODE: (THERE ARE PHP REQUEST BEFORE AND AFTER THIS CODE HENSE THE ELSEIF REQUEST)
-----------------------------------------------------------------------------------------------

} elseif ($_REQUEST["do"]=='add_photo') {
$temp = upload_image($HTTP_POST_FILES['photo']['tmp_name'], $_REQUEST["id"].'-'.$HTTP_POST_FILES['photo']['name'], $OPTIONS["path_accommodations"], $OPTIONS["pic_width"],$OPTIONS["pic_height"], '');

if ($temp==1) {
$sql = "INSERT INTO ".$TABLES["accommodations_photos"]."
SET accommodation_id='".$_REQUEST["id"]."',
filename='".$_REQUEST["id"].'-'.$HTTP_POST_FILES['photo']['name']."',
description='".mysql_escape_string($_REQUEST["description"])."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
};
$_REQUEST["do"] = 'edit_photos';

} elseif ($_REQUEST["do"]=='delete_photo') {


$sql = "SELECT * FROM ".$TABLES["accommodations_photos"]." WHERE id='".$_REQUEST["pid"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$PHOTO = mysql_fetch_assoc($sql_result);
if (is_file($OPTIONS["path_accommodations"].$PHOTO["filename"])) {
unlink($OPTIONS["path_accommodations"].$PHOTO["filename"]);
};
$sql = "DELETE FROM ".$TABLES["accommodations_photos"]." WHERE id='".$_REQUEST["pid"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$service_MESSAGE = '<img src="images/info.gif" align="absmiddle"><strong><font color="red">Photo deleted.</font></strong><br><br>';
$_REQUEST["do"] = 'edit_photos';
unset($_REQUEST["pid"]);

------------------------------------------------------------------
SECOND PART OF CODE ON USER PAGE
--------------------------------------------------------

<?php
} elseif ($_REQUEST["do"]=='edit_photos') {
$sql = "SELECT * FROM ".$TABLES["accommodations"]." WHERE id='".$_REQUEST["id"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$ACCOMMODATION = mysql_fetch_assoc($sql_result);
?>
-------------------------
BIT OF TABLE LAYOUT HERE
-------------------------
<?php
$sql = "SELECT * FROM ".$TABLES["accommodations_photos"]." WHERE accommodation_id='".$_REQUEST["id"]."' ORDER BY id DESC";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$totalPhotos = mysql_num_rows($sql_result);
while ($PHOTOS = mysql_fetch_assoc($sql_result)) {
?>
-------------------------
BIT OF TABLE LAYOUT HERE
-------------------------
<?php
if (is_file($OPTIONS["path_accommodations"].$PHOTOS["filename"])) {
echo "<img src='".$OPTIONS["url_accommodations"].$PHOTOS["filename"]."' height=150px>";
};
?>&nbsp;
-------------------------
BIT OF TABLE LAYOUT HERE
-------------------------
<?php }; ?>
</table>

<?php
if ($totalPhotos<30) {
?>

<form action="myaccount.php" method="post" name="frm" enctype="multipart/form-data">
<input type="hidden" name="do" value="add_photo" />
<input type="hidden" name="id" value="<? echo $_REQUEST["id"]; ?>" />
<input type="hidden" name="pid" value="0" />
-------------------------
BIT OF TABLE LAYOUT HERE
-------------------------
<input type="submit" name="Submit2" value="Save" />
-------------------------
BIT OF TABLE LAYOUT HERE
-------------------------
</form>
<a name="bottom"></a>
<?php
} else {
?>
You've uploaded all the 30 photos allowed.
<?php
};
?>

=========================================================================

RESIZE FUNCTION:

=========================================================================

<?php


function resize_crop($new_width, $new_height, $IMAGE ){

$new_image = imagecreatetruecolor($new_width,$new_height);

$orig_image = imagecreatefromjpeg($IMAGE);

list($original_width, $original_height) = getimagesize($IMAGE);

$proportion = $new_width/$new_height;
$orig_proportion = $original_width/$original_height;
if ($proportion<$orig_proportion) {
$changed_height=round($original_width/$proportion);
$diff_height=round(($changed_height-$original_height)/2);
$temp_image = imagecreatetruecolor($original_width,$changed_height);
$transback = imagecolorallocatealpha($temp_image,255,255,255,0);
imagefilledrectangle($temp_image,0,0,$original_width,$changed_height,$transback);
imagecopy($temp_image,$orig_image,0,$diff_height,0,0,$original_width,$original_height);
$original_height=$changed_height;
} elseif ($proportion>$orig_proportion) {
$changed_width=round($original_height*$proportion);
$diff_width=round(($changed_width-$original_width)/2);
$temp_image = imagecreatetruecolor($changed_width,$original_height);
$transback = imagecolorallocatealpha($temp_image,255,255,255,0);
imagefilledrectangle($temp_image,0,0,$changed_width,$original_height,$transback);
imagecopy($temp_image,$orig_image,$diff_width,0,0,0,$original_width,$original_height);
$original_width=$changed_width;
} else {
$temp_image=$orig_image;
}

/*$scale = max($new_width/$orginal_width, $new_height/$orginal_height);

if ($scale < 1) {

$new_width = floor($scale*$orginal_width);

$new_height = floor($scale*$orginal_height);

};

*/

imagecopyresampled($new_image, $temp_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

imagecopyresampled($new_image, $new_image, 0, 0, 0, 0, $new_width, $new_height, $new_width, $new_height);

imagejpeg($new_image, $IMAGE, 75);

};





function upload_image($file, $filename, $folder, $width, $height, $keeporiginal) {

$return = 1;

$size = getimagesize($file);

if (is_uploaded_file($file) AND $size["mime"]=='image/jpeg') {

if (move_uploaded_file($file, $folder.$filename)) {

if ($keeporiginal<>'') {

copy($folder.$filename, $folder.$keeporiginal);

chmod($folder.$keeporiginal,0644);

};

chmod($folder.$filename,0644);

resize_crop($width, $height, $folder.$filename, 100 ); ///////////// CREATE image

chmod($folder.$filename,0644);

} else {

$return = 2;

};

} else {

$return = 3;

};

return $return;

};

====================================================

ANY HELP GREATLY APPRECIATED!

===========================================
==========================================

arvind gupta

1:53 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



For having the new filename use:

$filename = $id . '-' . date('dmYHi') . '.jpg';


where "$id" is as calculated by your script.

For altering the quality:

1. Create a image in memory from the uploaded image

$src = imagecreatefromjpeg($TmpFile));


2. Create another image in memory with the new size (reszed)

$tmp = imagecreatetruecolor($newwidth, $newheight));


3. Copy

imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height));


4. Save

imagejpeg($tmp, $filename, $quality);


Hope this gets your work done!

P.S.: You Should have only put the required code instead of the whole thing. That's why (I suppose) no answered. It gets on your nerves when someone puts the whole thing for you to read when they'd have taken the time to put what should be in and what should be not.

zorro

5:22 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Apologies for including all the code, I am not a programmer and wasn't sure what was needed and what wasn't!
Thanks very much for taking the time to reply Arvind, I will give, what you have said a go.
Cheers!

zorro

5:26 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Arvind can you tell me from where to where (in the code I have included above) I can delete and do not need to include and I will edit the message.
Many thanks!

zorro

9:53 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Hi there, managed to get the image filename part working ok all files being named id-datetime.jpg

As for the 2nd part reducing the quality I simply do not have a clue which part to alter/edit/change/replace?

rocknbil

6:45 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Also we would like to reduce the orginal quality slightly to reduce the file size!


imagejpeg($new_image, $IMAGE, 75);

That third parameter is the compression to quality ratio.

Note that your script only supports jpg images, it's easy to add functions for other types. There's a recent post here, can't find it ATM . . . .

zorro

8:44 pm on May 7, 2010 (gmt 0)

10+ Year Member



Thanks for that Rocknbil.

Yes it was simply a matter of changing the number 75 to something smaller to reduce the quality. Thanks again!

Only thing is now the uploading of images has gone a bit hay wire.

Things worked ok when naming the files with the data and time:
$temp = upload_image($HTTP_POST_FILES['photo']['tmp_name'],
$_REQUEST["id"].'-'.date('dmYHi').'.jpg',

BUT IF A USER WAS UPLOADING MORE THAN ONE IMAGE WITHIN THE SAME MINUTE THINGS WENT A LITTLE CRAZY.

To prevent this I did a bit of searching on the net to see how to include Hours,Minutes and seconds so there could never be the same filename.

$temp = upload_image($HTTP_POST_FILES['photo']['tmp_name'],
$_REQUEST["id"].'-'.date('dmYHis').'.jpg',

As you can see all that wanted adding was the small letter 's' after 'dmYHi'

What is really strange now is that some images will load (all jpg) and some will not.
Or to be more specific the images do not show in the users area or my admin area for that matter even though the image filename is in the database and the actual image has been upload to the correct folder on the server.
How can some be referenced and not others (big scratch of head here).

The php code is also supposed to delete the image filename entry from the database and image from the image folder when a delete link is click but it doesn't for those image not showing in user or admin area ONLY FOR THOSE IMAGES THAT SHOW!

SO PUZZLED OVER THIS ONE!