Image crop and resize with php

Discussion in 'PHP' started by slaydragon, Jul 11, 2008.

  1. #1
    basically i have a large image 500x 500, i wana resize it to a smaller size, but the quality will stays, means image will not be pixelated or distorted. i am using php, below are my code, but i am having problems with it, would appreciated if some1 code help me out and give me some advices.

    $nw = 100;
    $nh = 100;
    $source = "http://img58.imageshack.us/img58/9644/400pxcodeblueip7.jpg";
    $stype = "jpg";
    $dest ="";
     
        $size = getimagesize($source);
        $w = $size[0];
        $h = $size[1];
     
        switch($stype) {
            case 'gif':
            $simg = imagecreatefromgif($source);
            break;
            case 'jpg':
            $simg = imagecreatefromjpeg($source);
            break;
            case 'png':
            $simg = imagecreatefrompng($source);
            break;
        }
     
        $dimg = imagecreatetruecolor($nw, $nh);
     
        $wm = $w/$nw;
        $hm = $h/$nh;
     
        $h_height = $nh/2;
        $w_height = $nw/2;
     
        if($w> $h) {
     
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;
     
            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
     
        } elseif(($w <$h) || ($w == $h)) {
     
            $adjusted_height = $h / $wm;
            $half_height = $adjusted_height / 2;
            $int_height = $half_height - $h_height;
     
            imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
     
        } else {
            imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }
     
        imagejpeg($dimg,$dest,100);
    PHP:
    i dont know why, when i preview it, it give me all kinds of weird coding.. like the below.

    ÿØÿàJFIFÿþ¿ËëŸ^õÂ9#hÝUÑÃ,Šê]a•”‚Y~òWGùý?ü—ፑgð7Ç_ÛWá¤ØýËø_öÄøñ®[Z€Ño¥üMñÄ‘s•†m6h°”(6Å’[¿Øö‡ÒAOÿÁM¿l &%É‚ÛǾý–¾([#à…ÜjŸ³þ¯\D>PÑË®‰J‚ ûœ¹ü9¨µñY5«”Z¶×º\Ï¿G¶Ú£õcÏ¿h/ØÃ…~ñö¡ûN~Áž2³øûA; Ïx&xÜ|øñ¿Ìú?ü-ú­ÜªýžÃÆ:]¼Z•›´iq$¶ŠÑWkû1ÁE¼ñ;Ã…ð¢h ê?³WíM¢2Xë_ |z~æx’ò?—ûKáÇŠ¦Xôoiš‰G¸ÓmínΡ=¶^Þ´Šicêô_€·ž-´:·íïáiVz†•vd¸ý¼#¦x‡P´´»©éz–«§|TFkmJÇ6âçLð¾—{krVæ;‚ŠÐ?ûlÛ~ÎÚ'ÃûOöÊñ/ìÇ©éâÊãKðíÏÄ?k'‹$ºŸ÷±ÙøFç@ñGˆ¼|·ò]$RÅÿU„º”S"Ïm›åµ q“ömº¶I§N2ºÕY>d›K{Y.ϲk[­žÖìú/^‡é¬n$Pàðyµ½9Ç9â£ò86x<ÿ\ñíõ Žy¯æ Á?·GíiðNÏö{ð‡ÃÏ‚~/ñ·ÁoøƒÇÞøauâgM‹ì¾O¨xjöûVÖþ \ø{ƺW‡|<ÏáýV{â-Ž…§Yé— ºþ¤ÈÚ2þ°]x³öÐø‡ðòêýü7ÿ úÛÅ>šëK¾ø9iðïÆ~4ÐãÕ´ã-þŸªüHø£áÿ¾³`'ŽaøGS±k˜Ê—
    Code (markup):
     
    slaydragon, Jul 11, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You need to set the Content-type...

    e.g.

    header('Content-type: image/png');
     
    Danltn, Jul 11, 2008 IP
  3. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi, so i add it above imagejpeg($dimg,$dest,100); ?
     
    slaydragon, Jul 11, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Yeah that should work, anywhere before something is output

    Dan
     
    Danltn, Jul 11, 2008 IP
  5. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hi, it give me this error.. Cannot modify header information - headers already sent by
     
    slaydragon, Jul 11, 2008 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    headers_sent($filename, $linenum);
    echo 'Headers sent at: ' , $filename , ' ' , $linenum;
    PHP:
    Remember, you must set headers before ANY output at all. Where's it sending output on yours? Move the header line above that one.
     
    Danltn, Jul 11, 2008 IP
  7. mussarath

    mussarath Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Folks


    This is Shaik
    Actually I have applied the image croppied functionality. Its working fine in local system, but when I apply the same things in server its not working.


    Can any one help me to resolve my issue.


    Thanks
     
    mussarath, Feb 24, 2012 IP
  8. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #8
    @mussarath - try not to duplicate your post or dig up really old threads, I've responded to your new one in the same forum. :)
     
    mfscripts, Feb 24, 2012 IP