Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    149
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Image script help

    Hello Team,

    I would like a script which lets me upload one big picture, and after i click submit it creates three different size pics, which it saves automatically to my hard drive. Can some one please help me create this script, or give me a start code.

    THank you for your time.

    Have a good day
    Genie1
    Tomorrow Could Be Your Day

  • #2
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    PHP Code:
        function createthumb($name,$filename,$new_w,$new_h){
            if (
    file_exists($filename)) { unlink($filename); }
            
    $system=explode('.',$name);
            if (
    preg_match('/jpg|jpeg/',$system[1])){
                
    $src_img=imagecreatefromjpeg($name);
            }
            if (
    preg_match('/png/',$system[1])){
                
    $src_img=imagecreatefrompng($name);
            }
            if (
    preg_match('/gif/',$system[1])){
                
    $src_img=imagecreatefromgif($name);
            }
            
    $old_x=imageSX($src_img);
            
    $old_y=imageSY($src_img);
            if (
    $old_x $old_y) {
                
    $thumb_w=$new_w;
                
    $thumb_h=$old_y*($new_h/$old_x);
            }
            if (
    $old_x $old_y) {
                
    $thumb_w=$old_x*($new_w/$old_y);
                
    $thumb_h=$new_h;
            }
            if (
    $old_x == $old_y) {
                
    $thumb_w=$new_w;
                
    $thumb_h=$new_h;
            }
                
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
                
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
            if (
    preg_match("/jpg|jpeg/",$system[1])) {
                
    imagejpeg($dst_img,$filename); 
            }
            if (
    preg_match("/png/",$system[1])) {
                
    imagepng($dst_img,$filename); 
            }
            if (
    preg_match("/gif/",$system[1])) {
                
    imagegif($dst_img,$filename); 
            }
            
    imagedestroy($dst_img); 
            
    imagedestroy($src_img); 
            
    chmod($filename,0644); // new file can sometimes have wrong permissions
        

    Call it with:

    createthumb(PATH TO FILE YOU WANT TO CREATE THUMB,PATH TO NEW FILE PLACE,NEW WIDTH,NEW HEIGHT);

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by sea4me View Post
    PHP Code:
        function createthumb($name,$filename,$new_w,$new_h){
            if (
    file_exists($filename)) { unlink($filename); }
            
    $system=explode('.',$name);
            if (
    preg_match('/jpg|jpeg/',$system[1])){
                
    $src_img=imagecreatefromjpeg($name);
            }
            if (
    preg_match('/png/',$system[1])){
                
    $src_img=imagecreatefrompng($name);
            }
            if (
    preg_match('/gif/',$system[1])){
                
    $src_img=imagecreatefromgif($name);
            }
            
    $old_x=imageSX($src_img);
            
    $old_y=imageSY($src_img);
            if (
    $old_x $old_y) {
                
    $thumb_w=$new_w;
                
    $thumb_h=$old_y*($new_h/$old_x);
            }
            if (
    $old_x $old_y) {
                
    $thumb_w=$old_x*($new_w/$old_y);
                
    $thumb_h=$new_h;
            }
            if (
    $old_x == $old_y) {
                
    $thumb_w=$new_w;
                
    $thumb_h=$new_h;
            }
                
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
                
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
            if (
    preg_match("/jpg|jpeg/",$system[1])) {
                
    imagejpeg($dst_img,$filename); 
            }
            if (
    preg_match("/png/",$system[1])) {
                
    imagepng($dst_img,$filename); 
            }
            if (
    preg_match("/gif/",$system[1])) {
                
    imagegif($dst_img,$filename); 
            }
            
    imagedestroy($dst_img); 
            
    imagedestroy($src_img); 
            
    chmod($filename,0644); // new file can sometimes have wrong permissions
        

    Call it with:

    createthumb(PATH TO FILE YOU WANT TO CREATE THUMB,PATH TO NEW FILE PLACE,NEW WIDTH,NEW HEIGHT);
    Which requires GD installed on your server
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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