Help Modifying a function

Discussion in 'PHP' started by billybrag, Sep 27, 2006.

  1. #1
    Hello All, I have inherited a script which uses the bel;ow function to deal with images that are uploaded.

    I just wondered if someone could help me. I need to stop it from converting the name into a random string and keep it as whatever is selected. I also change the size of the thumbnail that is also created and change its name from another random name to "(whatever is uploade)_thumb"

    Can anyone help me as the code makes little sense

    thanks very much

    Mike

    function uploadProductImage($inputName, $uploadDir)
    {
    	$image     = $_FILES[$inputName];
    	$imagePath = '';
    	$thumbnailPath = '';
    	
    	// if a file is given
    	if (trim($image['tmp_name']) != '') {
    		$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
    
    		// generate a random new file name to avoid name conflict
    		$imagePath = md5(rand() * time()) . ".$ext";
    		
    		list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 
    
    		// make sure the image width does not exceed the
    		// maximum allowed width
    		if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
    			$result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
    			$imagePath = $result;
    		} else {
    			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
    		}	
    		
    		if ($result) {
    			// create thumbnail
    			$thumbnailPath =  md5(rand() * time()) . ".$ext";
    			$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
    			
    			// create thumbnail failed, delete the image
    			if (!$result) {
    				unlink($uploadDir . $imagePath);
    				$imagePath = $thumbnailPath = '';
    			} else {
    				$thumbnailPath = $result;
    			}	
    		} else {
    			// the product cannot be upload / resized
    			$imagePath = $thumbnailPath = '';
    		}
    		
    	}
    
    	
    	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
    }
    Code (markup):
     
    billybrag, Sep 27, 2006 IP
  2. adrianctn

    adrianctn Active Member

    Messages:
    64
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    98
    #2
    Hello there Mike. I think I can help you out:

    step1:( set it to save the image name)

    Change this
             
                    // generate a random new file name to avoid name conflict
    		$imagePath = md5(rand() * time()) . ".$ext";
    
    PHP:
    to this:
    
                    $imagePath = $image['name'];
    
    PHP:
    step2:( change thumbnail name)
    Change this
    
    	// create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";
    
    
    PHP:
    to this:
    
            $leftSide = substr($image['name'], 0, strrpos($image['name'], '.') + 1);
            // leftSide is the "image" string from "image.jpg" for example
    	$thumbnailPath =  $leftSide . "_thumb" . ".$ext";
    
    PHP:
    step3:( thumbnail size)
    set MAX_PRODUCT_IMAGE_WIDTH to your desired size

    NOTE: I can't guarantee it will work, it usually takes a little debuging.
    Regards,

    Adrian
     
    adrianctn, Sep 27, 2006 IP
    billybrag likes this.
  3. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Adrian, I will give it a try!!

    Mike
     
    billybrag, Sep 27, 2006 IP