1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Resize Problem Any suggestions?

Discussion in 'PHP' started by dean5000v, Jun 10, 2010.

  1. #1
    Hey,

    i am basically dynamically displaying images onto a page using PHP, the problem is i need them to displayed at a much smaller size and in proportion. here is a snippet of the code.

    	
    echo '<a href="'. $auction_link .'"><img src=';
    echo ((!empty($main_image)) ? 'thumbnail.php?pic=' . $main_image . '&w=' . $layout['hpfeat_width'] . '&sq=Y' : 'themes/' . $setts['default_theme'] . '/img/system/noimg.gif');
    
    echo '" height="80" width="60" border="0" alt="' .$item_details[$counter]['name'] . '"></a><br><br>';
    Code (markup):
    as you can see at the moment to resize the image i am having to add in the height="80" width="60" which means that some images are totally out of proporotion. can anyone think of a little hack around this?
     
    dean5000v, Jun 10, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    do a simple crop in thumbnail.php to the size 80x60
     
    stephan2307, Jun 10, 2010 IP
  3. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you have a snipprt of code to help me?
     
    dean5000v, Jun 10, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
  5. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6
    roopajyothi, Jun 10, 2010 IP
  7. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #7
    Usually I use this function:
    $sSource - file-source
    $sDest - file-destination
    $iMaxX - max width
    $iMaxY - max height
    $iRGB - the color of filling
    $iQuality - the quality of jpg image, that we get as a result
    */
    function SaveResized ($sSource, $sDest, $iMaxX, $iMaxY, $iRGB=0xFFFFFF, $iQuality=80){
      if (!file_exists($sSource)) return -1; //file not found
      $aImgSize = getimagesize($sSource);
      if ($aImgSize === false) return -2; //getimgsize error
      $sMime = strtolower(substr($aImgSize['mime'], strpos($aImgSize['mime'], '/')+1));
      $fcImageCreateFrom = "imagecreatefrom" . $sMime;
      if (!function_exists($fcImageCreateFrom)) return -3;  //unknown format
      $fXRatio = $iMaxX / $aImgSize[0];
      $fYRatio = $iMaxY / $aImgSize[1];
      $fRatio       = min($fXRatio, $fYRatio);
      $fUseRatio = ($fXRatio == $fRatio);
      $iNewWidth   = $fUseRatio  ? $iMaxX  : floor($aImgSize[0] * $fRatio);
      $iNewHeight  = !$fUseRatio ? $iMaxY : floor($aImgSize[1] * $fRatio);
      $iNewLeft    = $fUseRatio  ? 0 : floor(($iMaxX - $iNewWidth) / 2);
      $iNewTop     = !$fUseRatio ? 0 : floor(($iMaxY - $iNewHeight) / 2);
      $rImg = $fcImageCreateFrom($sSource);
      $rImgDest = imagecreatetruecolor($iMaxX, $iMaxY);
      imagefill($rImgDest, 0, 0, $iRGB);
      imagecopyresampled($rImgDest, $rImg, $iNewLeft, $iNewTop, 0, 0,
        $iNewWidth, $iNewHeight, $aImgSize[0], $aImgSize[1]);
      imagejpeg($rImgDest, $sDest, $iQuality);
      imagedestroy($rImg);
      imagedestroy($rImgDest);
      return true;
    }
    PHP:
     
    Nei, Jun 11, 2010 IP
  8. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Oops Post a Finished Sample Code hre!
    Else they may confuse RGB with iRGB and others :)
     
    roopajyothi, Jun 11, 2010 IP
  9. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #9
    OK. Here is the example of using this function:

      $myfil =$_FILES['photo']['tmp_name'];
      if (is_uploaded_file($myfil)) 
    		SaveResized ($myfil, "../images/content/brand_img$id.jpg",89, 30);
    PHP:
     
    Nei, Jun 11, 2010 IP
  10. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Oops!
    I mean this structure

    
    $iRGB - the color of filling
    $iQuality - the quality of jpg image, that we get as a result
    
    PHP:
     
    roopajyothi, Jun 11, 2010 IP
  11. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #11
    i thought you're a programmer, you don't even know how to use that function..
     
    gapz101, Jun 11, 2010 IP
  12. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Oops!
    If i need to solve this is the solution
    
    $iRGB='e8edf0';
    $iQuality=80;
    
    Code (markup):
    BTW: My Job is Not Programming or 24 Hours dedicated Helping!
    If you know also Post your Solution! :)

    Also here is another one http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
     
    Last edited: Jun 12, 2010
    roopajyothi, Jun 12, 2010 IP