resizeing images in PHP

Discussion in 'OT Technology' started by MSTRMOO, Jul 13, 2007.

  1. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    this code wont resize my god damn image :wtc:

    i think there is something wrong with 'if ($size = @getimagesize($fetch['pic1']))'.. i dont think its finding the image, and then trying to divide by 0.. at which point it just goes to:

    else
    {
    $w = 420;
    $h = 300;
    }

    anyone see whats wrong?

    Code:
    [SIZE=2]if (!empty($_GET['item']))[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]$q = mysql_query("SELECT * FROM items WHERE id = '{$_GET['item']}'");[/SIZE]
    [SIZE=2]if ($fetch = mysql_fetch_assoc($q))[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]#item image[/SIZE]
    [SIZE=2]if ($size = @getimagesize($fetch['pic1']))[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]if ($size[0] > 420)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]$ratio = 420 / $size[0];[/SIZE]
    [SIZE=2]$w = 420;[/SIZE]
    [SIZE=2]$h = $size[1] * $ratio;[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]else[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]$w = $size[0];[/SIZE]
    [SIZE=2]$h = $size[1];[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]}[/SIZE]
    [SIZE=2]else[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]$w = 420;[/SIZE]
    [SIZE=2]$h = 300;[/SIZE]
    [SIZE=2]}[/SIZE]
     
  2. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    I think getimagesize returns two values, not just one . . . here's my code that works:

    Code:
    list($x, $y) = getimagesize("$file");
            if($x > $y){
               $max = $x;
               $min = $y;
            }
            if($x <= $y){
               $max = $y;
               $min = $x;
            }
        $new_max_size = '205';
        $rate = $max/$new_max_size;
        $final_x = $x/$rate;
        $final_y = $y/$rate;
        if($final_x > $x) {
                $final_x = $x;
                $final_y = $y;
        }
        $final_x = ceil($final_x);
        $final_y = ceil($final_y);
    $image_p = imagecreatetruecolor($final_x, $final_y);
    $new_image = imagecreatefromjpeg($file);
    @imagecopyresampled($image_p, $new_image, 0, 0, 0, 0, $final_x, $final_y, $x, $y);
    imagejpeg($image_p, "tmp/$filename", 50);
     
  3. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    i just did this as a test.. and get

    Warning: getimagesize(): URL file-access is disabled in the server configuration in /home/content/p/r/e/prestigefurn/html/item2.php on line 57

    Warning: getimagesize(http://www.prestigehomedesign.com/items/flooring/engineered/B_AMBOO_CARMELIZED.jpg): failed to open stream: no suitable wrapper could be found in /home/content/p/r/e/prestigefurn/html/item2.php on line 57

    Code:
    list($x, $y) = getimagesize($fetch['pic1']); 
    [FONT=Courier New][SIZE=2]{
    if ($x > $y)
    {
    $x = 800;
    $y = 100;
    }
    else
    {
    $x = 100;
    $y = 800;
    }
    }
    [/SIZE][/FONT]
     
  4. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    It looks like you are opening the file over the web?! Are you?? Your SQL might be returning a full domain name with the pic name . . . my sql is returning folder name and (random) pic in that folder . . .
     
  5. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    I thought php used == for equality.
     
  6. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,575
    Likes Received:
    2
    it does within a comparative operator.
     
  7. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    its not a random pic.. this is just part of the script.. the pic is chosen according to the item number, which is specified earlier..
     
  8. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I've seen this error before. Your server is configured to disallow opening files by URL, presumably for security reasons. If the image lives on your server, refer to it by absolute path instead of by URL. If the image lives somewhere else, you'll need to find a workaround to get it on your server before doing anything with it.
     
  9. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    What he said . . . which is what I was hinting to up top there . .
     
  10. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    here is what I use. the "path" is simply the image name in the /images folder on my server.


    Code:
    $imagesource =  "images/".$_GET['path'];
    /* Get the dimensions of the source picture */
    $picsize=getimagesize($imagesource);
    $source_x = $picsize[0];
    $source_y  = $picsize[1];
    		if ($source_x > $source_y) {
    			$percentage = (640 / $source_x);
    		} else {
    			$percentage = (640 / $source_y);
    		} 
    		
    		$dest_x = round($source_x * $percentage);
    		$dest_y  = round($source_y  * $percentage); 
     
  11. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    ok i turned "allow_url_fopen" on in php.ini.. it works now..... is there anything negative that may come from this :dunno: what kind of security issues? :dunno:
     

Share This Page