1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Print function

Discussion in 'PHP' started by helleborine, Mar 8, 2020.

  1. #1
    I have this code which allows users to upload images and resize them. I would like instead of an upload, a fixed image from my server.
    SEMrush
    
    Upload Images Below
    
    (Web or Computer-Based)
    
    <form action="" method="post"  enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    
    
    COMPUTER-based image upload:<br />
    <input name="uploadedfile" type="file" /><br />
    WEB-based image upload (URL):<br />
    <input type="text" name="url" /><br />
    <input type="submit" value="Upload File" name="upload"/>
    </form><br />
    
    Images Uploaded This Session<br />
    
    (Click on Image to Resize and/or Print)<br />
    <?php
    if ($handle = opendir('images')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && strstr($file,session_id().'_thumb.')) {
                echo '<a href="?print='.$file.'"><img src="images/'.$file.'" /></a>';
            }
        }
        closedir($handle);
    }
    Code (markup):
    The user should click on a given image, and be taken directly with this image to the step "Click on Image to Resize and/or Print"

    Thanks!
     
    helleborine, Mar 8, 2020 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #2
    You need to list the files in the directory - if there aren't many that should be a relatively simple directory listing process. If you have lots you'll want KC Finder or something like it (write your own?)

    ref: https://sourceforge.net/projects/kcfinder/
     
    sarahk, Mar 8, 2020 IP
  3. JEET

    JEET Notable Member

    Messages:
    2,899
    Likes Received:
    244
    Best Answers:
    9
    Trophy Points:
    215
    #3
    Your question is not at all clear.
    Do you want to show a default image instead of user uploaded image in the "print" link?
    or do you want code to upload image using this form?
     
    JEET, Mar 8, 2020 IP
  4. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #4
    I want to show a thumbnail of the image.
    When the user clicks on the thumbnail, the full size version is fetched "uploaded" and is then processed by further code before printing (over several pages if oversize). I hope that's possible.
     
    helleborine, Mar 8, 2020 IP
  5. JEET

    JEET Notable Member

    Messages:
    2,899
    Likes Received:
    244
    Best Answers:
    9
    Trophy Points:
    215
    #5
    PHP cannot show thumbnail of image "before" uploading.
    You will have to upload the image to a folder,
    create a smaller thumbnail using the uploaded image, (probably in a different folder),
    only then you can display thumbnail, and link it to the print page.
     
    JEET, Mar 8, 2020 IP
  6. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #6
    This is joke code but maybe it'll make it clearer.

    <img src="thumbnail.jpg" onclick=(get image.jpg already on website)>
    PRINT BUTTON then this image gets processed by php for special printing.
     
    helleborine, Mar 8, 2020 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #7
    I get what you're after.
    Did you look at the demos of KC Finder?
    Does it do what you're after as far as selecting an image on your server?
    Where does it fall short?
     
    sarahk, Mar 8, 2020 IP
  8. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #8
    <?php
    
        session_start();
       
        // SETTINGS
        $thumbwidth = 100;
        $deleteDays = 1;
       
        $size['letter']['width'] = 21.59;
        $size['letter']['height'] = 27.94;   
       
        $size['a4']['width'] = 21.0;
        $size['a4']['height'] = 29.7;   
    
    
        // A4     210 x 297
        // Letter 215.9 x 279.4
        // 1 inch = 2.54 cm
    
        $inch = 2.54;
       
        $border = 2.54;
       
        function make_thumb($src,$dest,$desired_width) {
           
             $arr = getimagesize($src);
            /* read the source image */
            switch ($arr['mime']) {
                case 'image/jpeg' : $source_image = imagecreatefromjpeg($src); break;
                case 'image/png' : $source_image = imagecreatefrompng($src); break;           
                case 'image/gif' : $source_image = imagecreatefromgif($src); break;              
                default: $ext = '';
            }
           
            $width = imagesx($source_image);
            $height = imagesy($source_image);
       
           /* find the "desired height" of this thumbnail, relative to the desired width  */
            $desired_height = floor($height*($desired_width/$width));
       
            /* create a new, "virtual" image */
            $virtual_image = imagecreatetruecolor($desired_width,$desired_height);
       
            /* copy source image at a resized size */
            imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
          
           switch ($arr['mime']) {
                case 'image/jpeg' : imagejpeg($virtual_image,$dest); break;
                case 'image/png' : imagepng($virtual_image,$dest); break;           
                case 'image/gif' : imagegif($virtual_image,$dest); break;              
                default: $ext = '';
            }
            /* create the physical thumbnail image to its destination */
            imagejpeg($virtual_image,$dest);
        }
    
    
        if(isset($_POST['upload']) && $_POST['url']=='') {
    
            $target_path = "images/";
                   
            $large = $target_path . date('YmdHis').'_'.session_id().'.'.substr(basename( $_FILES['uploadedfile']['name']),-3);
            $thumb = $target_path . date('YmdHis').'_'.session_id().'_thumb.'.substr(basename( $_FILES['uploadedfile']['name']),-3);        
           
    
            if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $large)) {
                make_thumb($large,$thumb,$thumbwidth);
            }
    
        }
        elseif (strlen($_POST['url'])>0) {
            $arr = getimagesize($_POST['url']);
    
            $ext = '';
            switch ($arr['mime']) {
                case 'image/jpeg' : $ext = 'jpg'; break;
                case 'image/png' : $ext = 'png'; break;           
                case 'image/gif' : $ext = 'gif'; break;              
                default: $ext = '';
            }
           
            if ($ext != '') {
                $file = 'images/'.date('YmdHis').'_'.session_id();
                $fh = fopen($file.'.'.$ext,'w+');
                $img = file_get_contents($_POST['url']);
                fwrite($fh,$img);
                fclose($fh);
                make_thumb($file.'.'.$ext,$file.'_thumb.'.$ext,$thumbwidth);
            }
        }
    ?>
    
    
            if (ratio == 'yes') {
                document.getElementById('imgheight').readOnly = true;
                 <?php
            if (isset($_GET['print'])) {
                $arr = getimagesize('images/'.str_replace('_thumb','',$_GET['print']));
               
                echo 'x = '.$arr[0].';';
                echo 'y = '.$arr[1].';';           
            }
         ?>
             width = document.getElementById('imgwidth').value;
             one = x/width;
             height = y/one;
             if (height > 0) {
                 document.getElementById('imgheight').value = height.toFixed(2);
            }
              
            }
            else {
                document.getElementById('imgheight').readOnly = false;           
            }
        }
       
        function calc() {
             if(document.getElementById('imgheight').readOnly == true) {
         <?php
            if (isset($_GET['print'])) {
                $arr = getimagesize('images/'.str_replace('_thumb','',$_GET['print']));
               
                echo 'x = '.$arr[0].';';
                echo 'y = '.$arr[1].';';           
            }
         ?>
             width = document.getElementById('imgwidth').value;
             one = x/width;
             height = y/one;
             if (height > 0) {
                 document.getElementById('imgheight').value = height.toFixed(2);
            }
             }
        }
    </script>
    
    
    <?php
    if (count($_GET)==0) {
    ?>
    
    <h1>FULL SIZE PRINTER/RESIZER<br></h1>
    
    
    <FONT COLOR="#33CCFF" SIZE=5>
    <b>Upload Images Below<br>
    
    <FONT COLOR="#33CCFF" SIZE=4>
    (Web or Computer-Based)</h2></font>
    <br />
    <form action="" method="post"  enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <FONT COLOR="#FFFFCC" SIZE=4>
    <br>
    COMPUTER-based image upload:<br />
    <input name="uploadedfile" type="file" /><br /><br />
    
    WEB-based image upload (URL):<br />
    <input type="text" name="url" /><br /><br />
    
    <input type="submit" value="Upload File" name="upload"/>
    
    </form>
    <br />
    <FONT COLOR="#33CCFF" SIZE=5>
    Images Uploaded This Session<br />
    <FONT COLOR="#33CCFF" SIZE=4>
    (Click on Image to Resize and/or Print)</font><br />
    <?php
    if ($handle = opendir('images')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && strstr($file,session_id().'_thumb.')) {
                echo '<a href="?print='.$file.'"><img src="images/'.$file.'" /></a>';
            }
        }
        closedir($handle);
    }
    
    }
    else if (isset($_GET['print'])) {
        echo '<img src="images/'.$_GET['print'].'" />';
        ?>
        <form action="?printview" method="post">
        <input type="hidden" name="image" value="<?php echo $_GET['print'];?>" />
        <p style="display: none;">Paper size: <select name="format"><option value="letter" selected>Letter</option><option value="a4">A4</option></select></p>
        <p>Image Ratio: <input type="radio" name="ratio" value="yes" checked onClick="manage(this.value);"/>Keep   <input type="radio" name="ratio" value="no" onClick="manage(this.value);"/>Ignore</p>               
        <p  style="display: none;">Image measurements: <select name="meassure"><option value="in" selected>inch</option><option value="cm">cm</option></select></p>
        <p>Image Width: <input type="text" name="width" id="imgwidth" onKeyUp="calc();"/></p>
        <p>Image Height: <input type="text" name="height" id="imgheight" readonly/></select></p>
    
        <input type="submit" />  
        </form>
        <?php
    }
    elseif(isset($_GET['printview'])) {
    
    ?>
    
    <img src="images/<?php echo $_POST['image'];?>" /><br />
    <input type="button" onClick="javascript:window.print();" value="Print the large Image" />
    </div>
    <?php
    }
    elseif(isset($_GET['delete'])) {
        if ($handle = opendir('images')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $parts = explode('_',$file);
               
                if ($parts[0]<date('YmdHis',strtotime('-'.$deleteDays.' days'))) {
                    unlink ('images/'.$file);
                }
            }
        }
        closedir($handle);
    }
    }
    ?>
    
    <?php
    
    // PHOTO PRINT END
    
    
    ?>
    
    
    </div>
    <?php
    if(isset($_GET['printview'])) {
       
     
       
        $_POST['image'] = str_replace('_thumb','',$_POST['image']);
       
       
       
        if ($_POST['meassure']=='in') {
            $imgWidth = $_POST['width']*$inch;
            $imgHeight = $_POST['height']*$inch;       
        }
        else {
            $imgWidth = $_POST['width'];  
            $imgHeight = $_POST['height']*$inch;       
        }
       
        if ($_POST['format']=='letter') {
            $maxPageWidth = $size['letter']['width'] - (2*$border);
            $maxPageHeight = $size['letter']['height'] - (2*$border);       
        }
        else {
            $maxPageWidth = $size['a4']['width'] - (2*$border);
            $maxPageHeight = $size['a4']['height'] - (2*$border);       
        }
       
    // echo "$imageX = ceil($imgWidth/$maxPageWidth);
       // $imageY = ceil($imgHeight/$maxPageHeight); ";
        $imageX = ceil($imgWidth/$maxPageWidth);
        $imageY = ceil($imgHeight/$maxPageHeight);
    //    echo '<p>'.$imageX.':'.$imageY.'</p>';
       
        $w = ($size[$_POST['format']]['width']-(2*$border)).'cm';
        $h = ($size[$_POST['format']]['height']-(2*$border)).'cm';
       ?>
        <style type="text/css">
    
    @media print{
    
        .img {
            height: <?php echo $h;?>;
            width: <?php echo $w;?>;   
            overflow: hidden;      
            float:left;
            page-break-before: always;
        }
        .fimg {
            height: <?php echo $h;?>;
            width: <?php echo $w;?>;   
            overflow: hidden;      
            float:left;
        }
       
         body {
            background-image: none;
            background-color: #ffffff;
        }
    
        #canvas, #preview {
            display: none;
        }
    
    }
    
    @media screen{
    
        .img {
            height: <?php echo $h;?>;
            width: <?php echo $w;?>;   
            overflow: hidden;      
            float:left;
            page-break-before: always;
        }
    
        .fimg {
            height: <?php echo $h;?>;
            width: <?php echo $w;?>;   
            overflow: hidden;      
            float:left;
        }
    
    
    
        #printing { display: none;
        }
    }
    
    
    
    </style>
    
    <div id="printing">
        <?php
    
       
        for($x=0; $x<$imageX; $x++) {
            for ($y=0; $y<$imageY; $y++) {
                if ($x==0 && $y==0) {
                    echo '<div class="fimg">';
                }
                else {
                    echo '<div class="img">';  
                }
               
                echo '
    <img src="images/'.$_POST['image'].'" style="width: '.$_POST['width'].$_POST['meassure'].';height: '.$_POST['height'].$_POST['meassure'].'; position: relative; top: -'.($y*$maxPageHeight).'cm; left: -'.($x*$maxPageWidth).'cm" /></div>';
            }
        }
       
       echo '</div>';
    
       
    }
    
    ?>
    
    
    Code (markup):
     
    helleborine, Mar 9, 2020 IP
  9. JEET

    JEET Notable Member

    Messages:
    2,899
    Likes Received:
    244
    Best Answers:
    9
    Trophy Points:
    215
    #9
    Put this in the <head> section of your HTML.

    <script type="text/javascript">
    function showImage( id, file ){

    f= file.replace( "_thumb", "" );
    f= "<img src='images/" + f +"?r="+ (Math.random()*100) + "' />";
    document.getElementById( id ).innerHTML = f;

    }
    </script>

    Then in PHP code, edit this code:

    <?php
    $count=1;
    if ($handle = opendir('images')) {
    while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != ".." && strstr($file,session_id().'_thumb.')) {

    ++$count;
    $id= 'i'.$count;
    $large= str_replace( '_thumb', '', $file );

    echo '<a onmouseover=" showImage( \''.$id.'\', \''.$file.'\' ) " href="?print='.$large.'">
    <img src="images/'.$file.'" /></a>
    <div id="'.$id.'"></div>
    ';
    }
    }
    closedir($handle);
    }


    This will display the thumb nail for all uploaded images in the session.
    When user will move their mouse over the thumbnail image, a larger image will be shown just below the small thumbnail.
    When they will click the thumbnail, then they will be taken to your print page where they will see larger image and can choose print settings.


    I hope I understood you correctly...
     
    JEET, Mar 9, 2020 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #10
    I've been sent the links and taken a look.

    upload_2020-3-10_10-12-10.png

    Is the main problem adapting the script you're using so that it works the way the old one does?

    print.php shows that you know how to list the contents of the directory, add a print button etc.

    1round.html is focussed on uploads and displaying the user's existing uploads.

    It should be relatively straight forward to add the new functionality - but 1round.html will need to become 1round.php
     
    sarahk, Mar 9, 2020 IP
  11. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #11
    How would the printing work?
     
    helleborine, Mar 10, 2020 IP
  12. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #12
    That doesn't change from how you're currently doing it.
     
    sarahk, Mar 10, 2020 IP
    JEET likes this.
  13. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #13
    OK, will try now.
     
    helleborine, Mar 10, 2020 IP