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 14 of 14

Thread: image resizing

  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    image resizing

    I would like to have a program that changes my images to a size specially made for a specified screen size. i don't wan't to have one image for every screensize, because it takes too much space on the server and I don't wan't to adjust one image for 20 other ones.

    Therefore I was thinking of resizing the images of php, which I know how I should do it, but I don't know how I can output it to a specific place in my html.

    Can I output this resized image to a table cell for example and make an image map of it?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I can help with the resize and placement part ...

    You have your HTML file with something like this:

    <table>
    <tr>
    <td><img src="resize.php?photo=myhouse.jpg"></td>
    </tr>
    </table>


    Then, you have a PHP script called "resize.php" ...
    PHP Code:
    <?php
    // The file
    $filename $_REQUEST['photo'];
    $wid=135;
    $hei=135;

    // Content type
    header('Content-type: image/jpeg');
    header('Content-length: '.filesize($filename));

    // Get new dimensions
    list($width$height) = getimagesize($filename);
    $new_width $wid;
    $new_height $hei;

    // Resample
    $image_p imagecreatetruecolor($new_width$new_height);
    $image imagecreatefromjpeg($filename);
    imagecopyresampled($image_p$image0000$new_width$new_height$width$height);

    // Output
    imagejpeg($image_pnull100);
    ?>

    Wherever you put this: <img src="resize.php?photo=myhouse.jpg">
    The image will appear. This size is set within the script, but you could
    create two new variables for the "width" and "height" and send them
    to the script.
    <img src="resize.php?photo=myhouse.jpg&w=150&h=200">

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this seems to return a number instead of a real image,
    see blommethomas.downfire.com for details

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I see one problem ...

    The PHP script is using .jpg not .png type images.

    Also ... I'm not sure about these lines:

    <META http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="date" content="2007-02-04T21:03:12+0100">
    <meta http-equiv="Content-Script-Type" content="text/javascript">

    What is that red line all about?
    You already describe the content type two lines above it.
    .... and this is odd: http-equiv="Content-Script-Type"

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the Content-Script-Type line was automatically generated and it should normally give no problems. I need javascript for further manipulation of the webpage

    main.php:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <HTML>
    <HEAD>
    <TITLE>Homepage of Thomas Blomme</TITLE>
    <META name="Author" lang="en" content="Thomas Blomme">
    <META name="keywords" lang="en" content="homepage, java, netbeans, JDance,linux, gentoo, mysql, php"> 
    <META http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="date" content="2007-02-04T21:03:12+0100">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    </HEAD>
    <BODY>
    <?php
    //initialisatie van enkele benodigde variabelen
    include("properties.inc");
    $multiplier $width 0.90/$menubarWidth;
    ?>

    <TABLE width="100%" border="0" align="center">
    <TR>
    <TD colspan="2">
    <?php
    echo "<img src='functions/resize.php?picture=menubar_" $lang ".png" +
     
    "&multiplier=" $multiplier "&imageWidth=" $menubarWidth 
     
    "&imageHeigth=" $menubarHeigth +
    "' usemap='menubar' border='0'></img>";
    ?>

    </TD>
    </TR>
    </TABLE>

    </BODY>
    </HTML>
    properties.inc:
    PHP Code:
    <?php
    $menubarWidth 
    1000;
    $menubarHeigth 50;
    $numberOfMenubarLinks 8;
    $menubarItem = array (=> "home""biography""music""pictures""linux",
    "games""java""links");
    $nlAltMenubarItem =array (=> "start""biografie""muziek""foto's""linux",
    "spelletjes""java""links");

    $bannerWidth 1000;
    $bannerHeigth 50;
    $numberOfBannerLinks 6;
    $bannerItem = array (=> "apache""mysql""php""java""netbeans""gentoo");

    ?>
    functions/resize.php
    PHP Code:
    <?php
    //parameters: picture, multiplier, imageWidth, imageHeigth
    include(../properties.inc");

    $width = $imageWidth . $multiplier;
    $heigth = $imageHeigth . $multiplier;

    header('Content-type: image/jpeg');
    header('Content-length: '.filesize($picture));

    $image = imageCreateFromPNG($picture);
    imageCopyResized($pictureResized, $picture, 0, 0, 0, 0, $width, $height, 
    $imageWidth, $imageHeight);

    imagePNG($pictureResized, null, 100);
    ?>

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I'm just stabbing at straws now ...

    How about this line?

    header('Content-type: image/jpeg');

    should that be:

    header('Content-type: image/png');

    ... more info ...
    http://www.fileformat.info/info/mime.../png/index.htm

    =======================

    Another thought ...

    Make a complete duplicate of your code in another directory and
    this time, use all JPEG images, and code (no PNG stuff). See if it
    works with JPEG. Perhaps this is just a PNG issue.


    .
    Last edited by mlseim; 02-08-2007 at 10:23 PM.

  • #7
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by mlseim View Post
    Make a complete duplicate of your code in another directory and
    this time, use all JPEG images, and code (no PNG stuff). See if it
    works with JPEG. Perhaps this is just a PNG issue.
    Great code and something I was looking for to generate thumbnails. The code from mlseim's first post works perfectly with jpeg's.
    Leonard Whistler

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I originally used this script when I helped someone
    out with a small --> full-size view ... (magnifying glass)

    Here's the experimental site we were working on:

    http://www.catpin.com/grillz

    again, it's all JPEG (not PNG)

  • #9
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'll give jpeg a try

  • #10
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the photo variable you give to the function is relative to the path of the new function and not to that of the page who calls the function, if my logic is right. Can someone confirm that?

  • #11
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Even the most basic script doesn't work, I have copy/pasted the above and I'm using jpeg now:

    blommethomas.downfire.com/test.html:
    Code:
    <table>
    <tr>
    <td><img src="functions/resize.php?photo=language_nl.jpeg"></td>
    </tr>
    </table>
    blommethomas.downfire.com/functions/resize.php:
    Code:
    <?php
    // The file
    $filename = $_REQUEST['photo'];
    $wid=135;
    $hei=135;
    
    // Content type
    header('Content-type: image/jpeg');
    header('Content-length: '.filesize($filename));
    
    // Get new dimensions
    list($width, $height) = getimagesize($filename);
    $new_width = $wid;
    $new_height = $hei;
    
    // Resample
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
    // Output
    imagejpeg($image_p, null, 100);
    ?>
    The image is both in the root directory and in the functions directory


    When accessing the function directly:
    <font face=verdana color=000000 ><br />
    <b>Fatal error</b>: Call to undefined function: imagecreatetruecolor() in <b>/www/users/blommethomas.downfire.com/functions/resize.php</b> on line <b>17</b><br />
    </font>
    Last edited by blommethomas; 02-13-2007 at 09:10 PM. Reason: found error

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Maybe there's something wrong or not installed with your GD Library?

  • #13
    New Coder
    Join Date
    Jan 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you mean of the server I'm using?

    Is this a functions specific to php5, as it might be possible my server uses php4

    And is this function really necessary to perform the script?

  • #14
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,092
    Thanks
    2
    Thanked 322 Times in 314 Posts
    PHP by itself is only a programming language. To manipulate images it relies on an external library of functions. The imagecreatetruecolor() function - http://www.php.net/manual/en/functio...etruecolor.php - as it states in the PHP manual section for that function, is part of the GD library - http://www.php.net/manual/en/ref.image.php. The GD library must be present and enabled within your PHP installation in order to use the functions it provides, as indicated in the error message you received - "Fatal error: Call to undefined function: imagecreatetruecolor()"
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    LinkBacks (?)

    1. 06-09-2014, 03:04 PM

    Posting Permissions

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