how to resize image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kujtim
    New Member
    • Sep 2007
    • 11

    how to resize image

    i got very hight resolution images . i would like to resize it to 800 with 600 px without using image editor is there anything like this on php something simple

    please help..
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by kujtim
    i got very hight resolution images . i would like to resize it to 800 with 600 px without using image editor is there anything like this on php something simple

    please help..
    Yup, i have a code that should work just fine for you - providing you have a website or winamp on localhost, or something.

    Save this as 'imgsize.php'

    [code=php]
    <?php
    header ("Content-type: image/jpeg");
    /*
    JPEG / PNG Image Resizer
    Parameters (passed via URL):

    img = path / url of jpeg or png image file

    percent = if this is defined, image is resized by it's
    value in percent (i.e. 50 to divide by 50 percent)

    w = image width

    h = image height

    constrain = if this is parameter is passed and w and h are set
    to a size value then the size of the resulting image
    is constrained by whichever dimension is smaller

    Requires the PHP GD Extension

    Outputs the resulting image in JPEG Format

    By: Michael John G. Lopez - www.sydel.net
    Filename : imgsize.php
    */

    $img = $_GET['img'];
    $percent = $_GET['percent'];
    $constrain = $_GET['constrain'];
    $w = $_GET['w'];
    $h = $_GET['h'];

    // get image size of img
    $x = @getimagesize($ img);
    // image width
    $sw = $x[0];
    // image height
    $sh = $x[1];

    if ($percent > 0) {
    // calculate resized height and width if percent is defined
    $percent = $percent * 0.01;
    $w = $sw * $percent;
    $h = $sh * $percent;
    } else {
    if (isset ($w) AND !isset ($h)) {
    // autocompute height if only width is set
    $h = (100 / ($sw / $w)) * .01;
    $h = @round ($sh * $h);
    } elseif (isset ($h) AND !isset ($w)) {
    // autocompute width if only height is set
    $w = (100 / ($sh / $h)) * .01;
    $w = @round ($sw * $w);
    } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
    // get the smaller resulting image dimension if both height
    // and width are set and $constrain is also set
    $hx = (100 / ($sw / $w)) * .01;
    $hx = @round ($sh * $hx);

    $wx = (100 / ($sh / $h)) * .01;
    $wx = @round ($sw * $wx);

    if ($hx < $h) {
    $h = (100 / ($sw / $w)) * .01;
    $h = @round ($sh * $h);
    } else {
    $w = (100 / ($sh / $h)) * .01;
    $w = @round ($sw * $w);
    }
    }
    }

    $im = @ImageCreateFro mJPEG ($img) or // Read JPEG Image
    $im = @ImageCreateFro mPNG ($img) or // or PNG Image
    $im = @ImageCreateFro mGIF ($img) or // or GIF Image
    $im = false; // If image is not JPEG, PNG, or GIF

    if (!$im) {
    // We get errors from PHP's ImageCreate functions...
    // So let's echo back the contents of the actual image.
    readfile ($img);
    } else {
    // Create the resized image destination
    $thumb = @ImageCreateTru eColor ($w, $h);
    // Copy from image source, resize it, and paste to image destination
    @ImageCopyResam pled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
    // Output resized image
    @ImageJPEG ($thumb);
    }
    ??
    [/code]

    call a photo as <img src="imgsize.ph p?h=height__you _want&img=path_ to_image" />

    Sorted :)

    Comment

    • kujtim
      New Member
      • Sep 2007
      • 11

      #3
      this is not working my image name is foto1.jpg i am replacing it with image path butt i am not reciving anything can you tell my more clearly what should i do

      Comment

      • steven
        New Member
        • Sep 2006
        • 143

        #4
        Originally posted by kujtim
        this is not working my image name is foto1.jpg i am replacing it with image path butt i am not reciving anything can you tell my more clearly what should i do
        Perhaps, rather than providing complete code, it would make more sense if you read up the documentation to understand exactly how such code functions. PHP is documented extremely well and it's easy to search and for questions such as this, it should be your first stop, before asking others.

        The docs:
        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


        List of functions for images:


        You have two choices for image resizing in PHP. A simple resize (which can leave an image looking ugly), or resizing and resampling. The latter is likely what you want. Read about it here:
        Last edited by Atli; Oct 20 '07, 01:06 AM. Reason: Fixed links

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          As Steven rightfully pointed out, providing the complete code to solve your problem will be less helpful in the long run than pointing you in the right direction so you can solve it yourself.
          (Please keep that in mind when posting in the future, Markus.)

          Using the links Steven provided and the code Markus posted you should be able to device a solution to this. This is a common problem so there should be plenty of documentation.

          Try solving this yourself and post back if you are having any difficulties.

          Comment

          • kujtim
            New Member
            • Sep 2007
            • 11

            #6
            hey thnx for help . looks like i have problem with my local php server when i am runing the script i am reciving this error

            Fatal error: Call to undefined function: imagecreatefrom jpeg() in D:\Sport's\3901 \3901\html\imgs ize.php on line 28

            but when i have uploaded the scipt in my web server it was working do you have any idea why i am reciving this error ???

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Sorry for only providing the code. I'm not an expert and i knew that script worked and he wanted something, therfore, i gave him it.

              Sorry again...

              Comment

              • steven
                New Member
                • Sep 2006
                • 143

                #8
                Originally posted by kujtim
                hey thnx for help . looks like i have problem with my local php server when i am runing the script i am reciving this error

                Fatal error: Call to undefined function: imagecreatefrom jpeg() in D:\Sport's\3901 \3901\html\imgs ize.php on line 28

                but when i have uploaded the scipt in my web server it was working do you have any idea why i am reciving this error ???
                You will need GD to use the Image functions. Perhaps you have the module and simply need to edit your php.ini to enable it. Have a look in your config file.

                Comment

                Working...