Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    9

    Exclamation Unanswered: Upload, resize, rename image with PHP

    Hi, I found on internet one scripts, for resizing picture when upload on server. Now I wanna to change that scripts to make me one more copy of the picture when upload with thumbnail resolution and new name where wanna just add prefix "thumb_". Scripts looks :


    if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {

    // file needs to be jpg,gif,bmp,x-png and 6 MB max
    if (($_FILES["slika1"]["type"] == "image/jpeg" || $_FILES["slika1"]["type"] == "image/pjpeg" || $_FILES["slika1"]["type"] == "image/gif" || $_FILES["slika1"]["type"] == "image/x-png") && ($_FILES["slika1"]["size"] < 6000000))
    {


    // some settings
    $max_upload_width = 4256;
    $max_upload_height = 2832;

    // if user chosed properly then scale down the image according to user preferances
    if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
    $max_upload_width = $_REQUEST['max_width_box'];
    }
    if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
    $max_upload_height = $_REQUEST['max_height_box'];
    }


    // if uploaded image was JPG/JPEG
    if($_FILES["slika1"]["type"] == "image/jpeg" || $_FILES["slika1"]["type"] == "image/pjpeg"){
    $image_source = imagecreatefromjpeg($_FILES["slika1"]["tmp_name"]);
    }
    // if uploaded image was GIF
    if($_FILES["slika1"]["type"] == "image/gif"){
    $image_source = imagecreatefromgif($_FILES["slika1"]["tmp_name"]);
    }
    // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
    // if uploaded image was BMP
    if($_FILES["slika1"]["type"] == "image/bmp"){
    $image_source = imagecreatefromwbmp($_FILES["slika1"]["tmp_name"]);
    }
    // if uploaded image was PNG
    if($_FILES["slika1"]["type"] == "image/x-png"){
    $image_source = imagecreatefrompng($_FILES["slika1"]["tmp_name"]);
    }

    // lokacija slika
    $remote_file = "http://www.dbforums.com/images/".$_FILES["slika1"]["name"];
    imagejpeg($image_source,$remote_file,100);
    chmod($remote_file,0644);



    // get width and height of original image
    list($image_width, $image_height) = getimagesize($remote_file);

    if($image_width>$max_upload_width || $image_height >$max_upload_height){
    $proportions = $image_width/$image_height;

    if($image_width>$image_height){
    $new_width = $max_upload_width;
    $new_height = round($max_upload_width/$proportions);
    }
    else{
    $new_height = $max_upload_height;
    $new_width = round($max_upload_height*$proportions);
    }


    $new_image = imagecreatetruecolor($new_width , $new_height);
    $image_source = imagecreatefromjpeg($remote_file);

    imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
    imagejpeg($new_image,$remote_file,100);

    imagedestroy($new_image);
    }

    imagedestroy($image_source);

    }
    }


    With this scripts I get new resolution of upload picture, how to get one more copy with brand new resolution and prefix in the name "thumb_" ?

  2. #2
    Join Date
    Sep 2012
    Posts
    3
    if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "user_post")) {

    // file needs to be jpg,gif,bmp,x-png and 4 MB max
    if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
    {




    $max_upload_width = 700;
    $max_upload_height = 700;


    // if uploaded image was JPG/JPEG
    if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
    $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
    }
    // if uploaded image was GIF
    if($_FILES["image_upload_box"]["type"] == "image/gif"){
    $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
    }
    // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
    // if uploaded image was BMP
    if($_FILES["image_upload_box"]["type"] == "image/bmp"){
    $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
    }
    // if uploaded image was PNG
    if($_FILES["image_upload_box"]["type"] == "image/x-png"){
    $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
    }



    // Rename File & add IP ....BAM
    $random_digit=rand(000000000,999999999);
    $remote_file_org = "image_files/"."org_".$ip."_"."$random_digit".".jpg";
    $remote_file = "image_files/".$ip."_"."$random_digit".".jpg";
    $remote_file_thumb = "image_files/"."_".$ip."_"."$random_digit".".jpg";


    imagejpeg($image_source,$remote_file,100);
    chmod($remote_file,0644);

    imagejpeg($image_source,$remote_file_thumb,100);
    chmod($remote_file_thumb,0644);

    imagejpeg($image_source,$remote_file_org,100);
    chmod($remote_file_org,0644);

    // get width and height of original image
    list($image_width, $image_height) = getimagesize($remote_file);

    // Save Thumbnail Image

    $new_image_thumb = imagecreatetruecolor(200 , 150); // Change 200,150 to size of thumb you need.
    $image_source_thumb = imagecreatefromjpeg($remote_file_thumb);

    imagecopyresampled($new_image_thumb, $image_source_thumb, 0, 0, 0, 0, 200, 150, $image_width, $image_height);
    imagejpeg($new_image_thumb,$remote_file_thumb,100) ;
    imagedestroy($new_image_thumb);







    if($image_width>$max_upload_width || $image_height >$max_upload_height){
    $proportions = $image_width/$image_height;

    if($image_width>$image_height){
    $new_width = $max_upload_width;
    $new_height = round($max_upload_width/$proportions);
    }
    else{
    $new_height = $max_upload_height;
    $new_width = round($max_upload_height*$proportions);
    }

    // 700 Pixel Thumb


    $new_image = imagecreatetruecolor($new_width , $new_height);
    $image_source = imagecreatefromjpeg($remote_file);

    imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
    imagejpeg($new_image,$remote_file,100);

    // 200 Pixel Thumb





    imagedestroy($new_image);

    }

    imagedestroy($image_source);


    header("Location: index.php?upload_message=image uploaded&upload_message_type=success&show_image=". "image_files/"."_".$ip."_"."$random_digit".".jpg");
    exit;
    }
    else{
    header("Location: index.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
    exit;
    }
    }
    ?>

  3. #3
    Join Date
    Sep 2012
    Posts
    3
    Also add this to the top of your php file

    $ip=$_SERVER['REMOTE_ADDR'];

  4. #4
    Join Date
    Sep 2012
    Posts
    3
    This should give you three different sizes. Any questions, ask.

Posting Permissions

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