Renaming image files and reduce quality help needed

Discussion in 'PHP' started by texastez, Apr 24, 2010.

  1. #1
    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 on 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 from the ground up 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 unsuccessfully.

    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.

    Images 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;
    
    };
    PHP:
    ====================================================

    ANY HELP GREATLY APPRECIATED!!!

    ===========================================
    ==========================================
     
    Last edited: Apr 24, 2010
    texastez, Apr 24, 2010 IP