www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP images heavy load.

  1. #1
    Join Date
    Dec 2011
    Posts
    20

    PHP images heavy load.

    So I have a heavy load of total ~64 split images in <TD> background from the one source.jpg(which size 70KB). I don't know how to fix that, it seems that something wrong in my code ,please help me to solve that. Code of split images works fine, but loads those images for about ~15seconds.

    PHP Code:
    // img.php file for displaying images
    <?php
    $in_filename 
    'source.jpg';

    $xcord $_REQUEST['ggridx']-1;
    $ycord $_REQUEST['ggridy']-1;

    //list($width, $height) = getimagesize($in_filename);

    $map_grid_size 60;

    $image imagecreatefromjpeg($in_filename);
    $new_image imagecreatetruecolor($map_grid_size$map_grid_size);

    imagecopy($new_image$image00$xcord*$map_grid_size$ycord*$map_grid_size$map_grid_size$map_grid_size);

    header('Content-Type: image/jpeg');
    imagejpeg($new_image);
    imagedestroy($new_image);
    imagedestroy($image);

    ?>
    PHP Code:
    // Main file where I'm calling
        
    $map_display "";
        for (
    $Ty $start_y$Ty <= $end_y$Ty++) {
            
    $map_display .= "<tr>";
            for (
    $Tx $start_x$Tx <= $end_x$Tx++) {
                
    //show grid
                
    $map_display .= "<td style=\"background-image: url('img.php?ggridx=$Tx&ggridy=$Ty');\">";
                
    $map_display .= DisplayGrid($Tx,$Ty);
                
    $map_display .= "</td>";
            }
            
    $map_display .= "<tr/>";
        } 
    Last edited by xbatista; 04-25-2012 at 02:22 PM.

  2. #2
    Join Date
    Dec 2011
    Posts
    20
    Someone? ;0

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    I suspect it would be much more efficient to simply break that one image up into 64 separate image files, and just have your PHP code choose which one to use for each table cell, rather than doing 64 image manipulations on each page request. You could even use your existing PHP script as the basis of a script to do that, writing each image to a file instead of outputting it (using some naming convention to make it clear what each image is).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2011
    Posts
    1,144
    I'd suggest relying on CSS and using your background image like a Sprite. Let the browser do the work with (essentially) just one HTTP request for the image file with something like:
    Code:
        $map_display = "";
        for ($Ty = $start_y; $Ty <= $end_y; $Ty++) {
            $map_display .= "<tr>";
            for ($Tx = $start_x; $Tx <= $end_x; $Tx++) {
                //show grid
                $map_display .= "<td style=\"background-image: url('source.jpg'); ";
                $map_display .= "background-position:" . (($Tx-1) * $map_grid_size) . "px  " . (($Ty-1) * $map_grid_size) . "px;\">";
                $map_display .= DisplayGrid($Tx,$Ty);
                $map_display .= "</td>";
            }
            $map_display .= "</tr>";
        }
    I apologize for any syntax errors. I got a bit lost in the quotation marks, but it should be close. And be sure to correct the background image URL now that it's inline and not embedded in your PHP script.
    Rick Trethewey
    Rainbo Design

  5. #5
    Join Date
    Dec 2011
    Posts
    20
    Thanks for the answers!

    Ohh I always thought that you can't use coordinates x,y in background-position I'll try that

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    Quote Originally Posted by rtrethewey View Post
    I'd suggest relying on CSS and using your background image like a Sprite. Let the browser do the work with (essentially) just one HTTP request for the image file with something like...
    Good idea. I should have thought of that.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles