SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Swindon
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Modifying a function

    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

    Code:
    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);
    }

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    London
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Untested

    Hi, mike this is untested, have removed the random code replaced with image name.
    For the image resizing you will need to look for some constants inyour system.
    LIMIT_PRODUCT_WIDTH, MAX_PRODUCT_IMAGE_WIDTH, THUMBNAIL_WIDTH.
    PHP Code:
    function uploadProductImage($inputName$uploadDir)
    {
        
    $image     $_FILES[$inputName];
        
    $imagePath '';
        
    $thumbnailPath '';
        
        if (
    trim($image['tmp_name']) != '') {
            
    $imagePath =  $image['name'];
            list(
    $width$height$type$attr) = getimagesize($image['tmp_name']); 
            if (
    LIMIT_PRODUCT_WIDTH && $width MAX_PRODUCT_IMAGE_WIDTH) {
                
    $result    createThumbnail($image['tmp_name'], $uploadDir $imagePathMAX_PRODUCT_IMAGE_WIDTH);
                
    $imagePath $result;
            } else {
                
    $result move_uploaded_file($image['tmp_name'], $uploadDir $imagePath);
            }    
            if (
    $result) {
                
    $thumbnailPath =  $image['name'];
                
    $result createThumbnail($uploadDir $imagePath$uploadDir $thumbnailPathTHUMBNAIL_WIDTH);
                if (!
    $result) {
                    
    unlink($uploadDir $imagePath);
                    
    $imagePath $thumbnailPath '';
                } else {
                    
    $thumbnailPath $result;
                }    
            } else {
                
    $imagePath $thumbnailPath '';
            }    
        }
        return array(
    'image' => $imagePath'thumbnail' => $thumbnailPath);

    David Stevens, create-inspire
    PHP London, www.phplondon.org


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •