Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

black images with PHP

  • 15-11-2006 12:22AM
    #1
    Closed Accounts Posts: 32


    Hey. I have just finished coding an image gallery for my web site using PHP. I'm not much of a PHP hacker, but I've written stuff in Perl and there seems to be a reasonable amount of overlap.
    Anyway, my problem is that, when I generate thumbnails by dynamically resizing the .jpgs, some images come up completely black, while others do exactly what it says on the tin. I've spent ages hunting for a solution on the interweb and I can't find any (I've seen a load of people complain about it, but no good answers), so I thought I'd see if there's a PHP nerd among you who can help me out!
    I don't know much about jpeg images, but I think it might have something to do with them rather than with my code because the exact same code works perfectly for some images, but not for others.
    Anybody experience this before? Am I doing something completely stupid?


Comments

  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    Check out this code:
    if($_POST[Insert])
    {
    
    	$new_height=110;
    	$new_width=140;
    	$allowed_types = array('image/pjpeg','image/png','image/jpeg');
    	
    	if(in_array($_FILES['browseFiles']['type'], $allowed_types))
    	{
    		copy ($_FILES['browseFiles']['tmp_name'], $_FILES['browseFiles']['name']) or die ("Could not copy");
    		$imagefile=$_FILES['browseFiles']['name'];
    		list($width, $height) = getimagesize($_FILES['browseFiles']['name']);
    		$image_p = imagecreatetruecolor($new_width,$new_height);
    		
    		$fillcolour = imagecolorallocate($image_p, 255, 255, 225);
    		imagefill($image_p, 0, 0, $fillcolour );
    		
    		$ratio_orig = $width/$height;
    		
    		if ($new_width/$new_height > $ratio_orig) {
    		   $new_width = $new_height*$ratio_orig;
    		} else {
    		   $new_height = $new_width/$ratio_orig;
    		}
    				
    		$img = @imagecreatefromjpeg($imagefile);
    		imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    		
    		$thename=$_FILES['browseFiles']['name'];
    		$thenames="thumb_$thename";
    		$location="$directory/$thenames";
    		imagejpeg($image_p,$location, 100);
    		
    		$insertImageQuery = "UPDATE people SET photo='$thenames' WHERE id = '$_POST[peopleId]'";
    		
    		$result = mysql_query($insertImageQuery);
    		if (!$result) {
    echo "<script>alert('Error- Photo Upload Failed');</script>";			
    		}
    		else{
    			echo "<script>alert('Success - Photo Uploaded');</script>";
    		}		
    
    	}
    	else
    	{
    		echo "<Script>alert('Error! Please ensure that you image is in JPEG or JPG format');</script>";
    	}
    
    }
    

    In the code note the
    $fillcolour = imagecolorallocate($image_p, 255, 255, 225);
    		imagefill($image_p, 0, 0, $fillcolour );
    

    By default the background of the new image created by PHP is set to black. You need to set the background colour to the background colour of your webpage.


  • Closed Accounts Posts: 32 jctheimpaler


    Thanks for the reply. I specified the fillcolour to be white, but it didn't seem to make a difference...
    Check out this link:
    http://captaindrinkingbinge.com/index.php?source=photos/index.php
    The entire site is currently under construction, so it's not pretty yet...
    You can see on this photos page that the two album images are black. If you click on the first album (Burla Beach), you can see most of the images, except for number 9. What is that all about??


  • Closed Accounts Posts: 32 jctheimpaler


    Actually, it turns out I'm an idiot. Some of the filenames had illegal characters in them.
    Thanks for your help anyway!


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    I was about to suggest this when i red your post.
    Had similar issue myself a while back, when i discover exactly the same thing.
    Never thought that somebody will add & or % in the photo name.
    Anyway, I learn my lesson. I also strtolower("photoname") as well and replaced the spaces with "_" just to make sure.


Advertisement