Resize image problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evolozik
    New Member
    • Jan 2007
    • 4

    Resize image problem

    hi everyone
    i need some help please
    i am using PHP5 and i used a tutorial and when i copied the code for Resampling an image proportionally, it worked perfectly
    but when i added it in my page it displayed a lot of "ÿØÿàJFIFÿ þ‡úý<ÿyºÿ&Ïþ <ߗ׏©" characters

    here is what i did:
    [PHP]

    //some code

    function resizeimage($im ages)
    {
    // Retrieving path
    $filename = $images;
    // Set a maximum height and width
    $width = 100;
    $height = 100;

    // Get new dimensions
    list($width_ori g, $height_orig) = getimagesize($f ilename);

    $ratio_orig = $width_orig/$height_orig;

    if ($width/$height > $ratio_orig) {
    $width = $height*$ratio_ orig;
    } else {
    $height = $width/$ratio_orig;
    }

    // Resample
    $image_p = imagecreatetrue color($width, $height);
    $image = imagecreatefrom jpeg($filename) ;
    imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    // Output
    $new=imagejpeg( $image_p, null, 100);
    }

    [/PHP]


    then after some other code, i wrote:

    [PHP]
    $img=$newRow['picture'];
    echo "<a href=\"test1.ph p\"><img src=resizeimage ($img) border=0></a>";
    [/PHP]

    $newRow['picture'] is the path of the image saved in the database

    anyone has an idea of why it is not working properly?
  • cassbiz
    New Member
    • Oct 2006
    • 202

    #2
    Is your script using ImageMagick?

    if so, is it properly installed. You stated that it worked perfectly then after your changes it messed up or after you tried to resize an image without making any changes?

    Comment

    • evolozik
      New Member
      • Jan 2007
      • 4

      #3
      no i am not using Image Magick

      i got the tutorial from this website:
      http://www.php.net/manual/en/function.imagec opyresampled.ph p

      the only thing i changed is that i enclosed the code in a function and added the part below so that it retrieves the image and then it resize it using the function before display the picture in <img src...>

      [php]
      $img=$newRow['picture'];
      echo "<a href=\"test1.ph p\"><img src=resizeimage ($img) border=0></a>";
      [/php]

      i think there is something wrong with this part :
      <img src=resizeimage ($img) border=0>
      but i can't figure out what is missing or what i have put have i shouldn't have

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        This line in your code has not the wanted effect:[php]$new=imagejpeg( $image_p, null, 100);[/php] because imagejpeg() does not return a value, but just displays it onto the screen.
        If you do not want to display it right away but later, you must save it[php]$file_name="my_ file.jpg";
        imagejpeg($imag e_p, $file_name, 100);[/php]Then later in your script you can display the file using the <img> tag.

        Ronald :cool:

        Comment

        • evolozik
          New Member
          • Jan 2007
          • 4

          #5
          Originally posted by ronverdonk
          This line in your code has not the wanted effect:[php]$new=imagejpeg( $image_p, null, 100);[/php] because imagejpeg() does not return a value, but just displays it onto the screen.
          If you do not want to display it right away but later, you must save it[php]$file_name="my_ file.jpg";
          imagejpeg($imag e_p, $file_name, 100);[/php]Then later in your script you can display the file using the <img> tag.

          Ronald :cool:
          i added this part because i want it to display the picture right away
          [php]
          $new=imagejpeg( $image_p, null, 100);
          return $new;
          [/php]

          i understood what is wrong
          using the code below doesn't display the picture but is trying to display the "codes" of the picture
          [php]
          $img=$newRow['picture'];
          echo "<a href=\"test1.ph p\"><img src=resizeimage ($img) border=0></a>";
          [/php]

          so i change it to
          [php]
          $img=$newRow['picture'];
          echo "<a href=\"test1.ph p\">resizeimage ($img)</a>";
          [/php]

          and now it displays for example:
          resizeimage(ima ges/pic.jpg)

          is there a way that i can display the picture and not the link?

          Comment

          • evolozik
            New Member
            • Jan 2007
            • 4

            #6
            hey Ronald
            i figured out something

            if i put the code like this:


            [PHP]
            //some code

            function resizeimage($im ages)
            {
            // Retrieving path
            $filename = $images;
            // Set a maximum height and width
            $width = 100;
            $height = 100;

            // Get new dimensions
            list($width_ori g, $height_orig) = getimagesize($f ilename);

            $ratio_orig = $width_orig/$height_orig;

            if ($width/$height > $ratio_orig) {
            $width = $height*$ratio_ orig;
            } else {
            $height = $width/$ratio_orig;
            }

            // Resample
            $image_p = imagecreatetrue color($width, $height);
            $image = imagecreatefrom jpeg($filename) ;
            imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

            // Output
            $new=imagejpeg( $image_p, null, 100);
            }
            $img=$newRow['picture'];
            resizeimage($im g)
            [/PHP]
            in this way the picture appears
            but when i insert something between the function's closing bracket and $img then the weird characters appear
            what is wrong?

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Those are not 'weird' characters, they are the content of the image file.
              The last line of the function must destroy the image storage, so you must add statement[php]imagedestroy($i mage_p);[/php] at the end of the function.

              As I said before, the '$new' variable is empty because there is nothing returned. It displays it only and at the exact spot where you issue the imagejpeg() command, nothing else! So why don't you save it and later display it?

              Do not just copy samples, but complete the entire tutorial and you'll know.

              Ronald :cool:

              Comment

              Working...