How to resize images automatically

Discussion in 'PHP' started by jennypretty, Nov 7, 2007.

  1. #1
    Hello,

    I have a dating site. When members upload their images, how do I resize the images to be 300px width and let the height automatically?

    some images are large so they load too slow.

    Can you please help?

    Thanks,

    Jenny.

    p.s.

    Here is the upload image code block:
    <input name = "image_<? echo $i;?>" type = "text" id="image_<? echo $i;?>" value="<? echo $image[$i];?>" size="20" readOnly > 
                  <input   type=BUTTON name=btn_name<? echo $i;?> value="Upload" onClick=attachment('image_<? echo $i;?>')> 
    
    Code (markup):
     
    jennypretty, Nov 7, 2007 IP
  2. garbageman

    garbageman Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make a seperate php file that will resize the image...
    
    <?
    $file = "asdf.jpg";
    $w2 = "640";  // New dimensions
    $h2 = "480";
    
    header("Content-Type: image/png");
    $dot = explode ( '.', $file );
    $dot = array_reverse ( $dot );
    switch($dot[0]){
    case 'jpg':$image = imagecreatefromjpeg($file);break;
    case 'gif':$image = imagecreatefromgif($file);break;
    case 'png':$image = imagecreatefrompng($file);break;
    }
    list($w, $h) = getimagesize($file);
    $resized = imagecreatetruecolor($w2, $h2);
    imagecopyresized($resized, $image, 0, 0, 0, 0, $w2, $h2, $w, $h);
    imagedestroy($image);
    imagepng($resized);
    imagedestroy($resized);
    ?>
    
    PHP:
    Of course, your server will need to have gd and image functions installed.
     
    garbageman, Nov 7, 2007 IP
  3. jennypretty

    jennypretty Active Member

    Messages:
    584
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks for your help.
    What does this file asdf.jpg mean? is this a makeup file I choose any?
    The script you showed above will resize all images after members upload their images on my site, right?
    Thanks.
     
    jennypretty, Nov 8, 2007 IP
  4. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
  5. jennypretty

    jennypretty Active Member

    Messages:
    584
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #5
    I try to resize images for a folder that have more than 200 images. This script does one by one which is not what I am looking for.

    If one by one, I can use Photoshop to resize each image.

    Thanks.
     
    jennypretty, Nov 8, 2007 IP
  6. armatik

    armatik Peon

    Messages:
    27
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I've been using an image resizing function for a while, and it works well. Technically, it saves the picture as whatever dimensions it's uploaded to, but it limits the dimensions in which it is viewed on.

    Hope this helps. :)

    function resizeImage($image,$target) {
    	// Get the current image size
    	$image = getimagesize($image);
    
    	// Get percentage
    	if ($image[0] > $image[1]) {
    		$percent = $target / $image[0];
    	} else {
    		$percent = $target / $image[1];
    	}
    
    	// Get new width and height
    	$w = round($image[0] * $percent);
    	$h = round($image[1] * $percent);
    
    	// Return as an html
    	$htmlWH = "width=\"" . $w . "\" height=\"" . $h . "\"";
    	return $htmlWH;
    }
    PHP:
    And this is an example of how you would use it:

    resizeImage($row['image'], 200)
    PHP:
     
    armatik, Nov 8, 2007 IP
  7. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #7
    You just need to 'while' it ;)
     
    linkstraffic, Nov 9, 2007 IP
  8. jennypretty

    jennypretty Active Member

    Messages:
    584
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #8
    I am a newbie in php so I don't know how to write a while loop.
    Thanks.
     
    jennypretty, Nov 9, 2007 IP
  9. garbageman

    garbageman Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I can write a script to run through your images folder and convert them all to a certain size for a fee.
    After that, you may consider embedding the resize program into your upload script. It would be best to read the image into a variable from the temporary upload directory, resize it, and then save it to the final directory using imagejpeg();.
     
    garbageman, Nov 10, 2007 IP
  10. mystery

    mystery Banned

    Messages:
    744
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    If Anyone Is Looking For A Very Cool Image Script, That Will Literally BLOW YOU AWAY! They can PM me ;)
     
    mystery, Nov 11, 2007 IP
  11. jennypretty

    jennypretty Active Member

    Messages:
    584
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #11
    Can you please pm me the price?
    Thanks.
     
    jennypretty, Nov 15, 2007 IP