Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How do I get this function to work

Attached is a code that I'm told works. I can't test it however, because I don't know enough about functions and how to call them in standard PHP as opposed to OOP, and I'm not even sure this is what I need to do.

In any event, I've got to resize the images as they exist in my database so they display symetrically. As I say, the code below works, but I don't know how to implement it.

I'm looping through my recordset and I come up to here:

<table width="110" border="0">
                                                <tr>
                                                <td align="center">                              
                                                <A HREF="video_player.php?id=<?php echo $id; ?>" target="_blank"><IMG SRC="<?php echo stripslashes($video_thumbnail); ?>" border="0" width="100"></a>
                                                </td>
                                                </tr>
                                                <tr>
                                                <td align="center" class="video">
                                                <?php echo stripslashes($name); ?>
                                                </td>
                                                </tr>
                                                </table>

I've got to  resize my video_thumbnail. How do I take the attached script and incorporate it into my loop so I get a resized video_thumbnail?
function resize_image($source_pic, $destination_pic = "auto", $new_width, $new_height, $proprtion = false, $unlink_src = false){
    $src = imagecreatefromjpeg($source_pic);
    list($width,$height) = getimagesize($source_pic);

    $x_ratio = $new_width / $width;
    $y_ratio = $new_height / $height;

    if (proportions){
        if (($x_ratio * $height) < $new_height){
            $tn_height = ceil($x_ratio * $height);
            $tn_width = $new_width;
        }else{
            $tn_width = ceil($y_ratio * $width);
            $tn_height = $new_height;
        }
    }else{
        $tn_width = $new_width;
        $tn_height = $new_height;
    }

    $tmp=imagecreatetruecolor($tn_width,$tn_height);
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

    if ($destination_pic == "auto"){
        $filearr = explode(".", $source_pic);
        $basename = array_shift($filearr);
        $destination_pic = $basename."_".$tn_width."jpg";
    }

    imagejpeg($tmp,$destination_pic,100);
    imagedestroy($src);
    imagedestroy($tmp);

    if ($unlink_src){
        unlink($source_pic);
    }
}

Open in new window

Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

try
<?php
$tmp = resize_image($video_thumbnail, "auto", 100, 100, true, false);
?>
<A HREF="video_player.php?id=<?php echo $id; ?>" target="_blank">
<IMG SRC="<?php echo tmp; ?>" />
</a>

Avatar of Marco Gasi
Let me understand: you gave me 66 points as assisted solution but you use my script?! Please, request attention and award points accordingly with your choice.

About loop, I suggest you to loop using function and preparing image s to publish:

for ($i=0;$i<imagescount;$i++){
  resize_image($video_thumbnail, $dest_thumbnail_path_ and_name, 100, 75,  True);

<table width="110" border="0">
                                                <tr>
                                                <td align="center">                              
                                                <A HREF="video_player.php?id=<?php echo $id; ?>" target="_blank"><IMG SRC="<?php echo stripslashes($dest_thumbnail_path_ and_name); ?>" border="0" width="100"></a>
                                                </td>
                                                </tr>
                                                <tr>
                                                <td align="center" class="video">
                                                <?php echo stripslashes($name); ?>
                                                </td>
                                                </tr>
                                                </table>  
}

Hope this helps
Avatar of Bruce Gust

ASKER

I get this error:
Fatal error: Call to undefined function resize_image() in /home/content/b/r/u/brucegust/html/Texaco/video_gallery.php on line 95
Have you inserted image_resize function in php tags at the top of the page?
<?php
function resize_image($source_pic, $destination_pic = "auto", $new_width, $new_height, $proprtion = false, $unlink_src = false){
    $src = imagecreatefromjpeg($source_pic);
    list($width,$height) = getimagesize($source_pic);

    $x_ratio = $new_width / $width;
    $y_ratio = $new_height / $height;

    if (proportions){
        if (($x_ratio * $height) < $new_height){
            $tn_height = ceil($x_ratio * $height);
            $tn_width = $new_width;
        }else{
            $tn_width = ceil($y_ratio * $width);
            $tn_height = $new_height;
        }
    }else{
        $tn_width = $new_width;
        $tn_height = $new_height;
    }

    $tmp=imagecreatetruecolor($tn_width,$tn_height);
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

    if ($destination_pic == "auto"){
        $filearr = explode(".", $source_pic);
        $basename = array_shift($filearr);
        $destination_pic = $basename."_".$tn_width."jpg";
    }

    imagejpeg($tmp,$destination_pic,100);
    imagedestroy($src);
    imagedestroy($tmp);

    if ($unlink_src){
        unlink($source_pic);
    }
}
?>
// here the rest of your page: now you can call image_resize without error.

Cheers
Hai,

Find the attachment for the functions which i have used for the image upload and crop images - resize

very easy and works fine in all the situation
functions.php
Marcusg - I didn't give you full credit on the other question because one of the contributors made the point that the original question didn't ask for a resizing script (although that's what I ultimately needed). So, I gave you partial credit, opened up a new question that dealt with the resizing issue and there you go.

Here's the error I'm getting:

Warning: imagecreatefromjpeg(Finals2006.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/content/b/r/u/brucegust/html/Texaco/photo_test.php on line 3

My code is attached. I'm sure it's something simple, but it's kicking my tail right now.

Thanks...
<?php
function resize_image($source_pic, $destination_pic = "auto", $new_width, $new_height, $proprtion = false, $unlink_src = false){
    $src = imagecreatefromjpeg($source_pic);
    list($width,$height) = getimagesize($source_pic);

    $x_ratio = $new_width / $width;
    $y_ratio = $new_height / $height;

    if (proportions){
        if (($x_ratio * $height) < $new_height){
            $tn_height = ceil($x_ratio * $height);
            $tn_width = $new_width;
        }else{
            $tn_width = ceil($y_ratio * $width);
            $tn_height = $new_height;
        }
    }else{
        $tn_width = $new_width;
        $tn_height = $new_height;
    }

    $tmp=imagecreatetruecolor($tn_width,$tn_height);
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

    if ($destination_pic == "auto"){
        $filearr = explode(".", $source_pic);
        $basename = array_shift($filearr);
        $destination_pic = $basename."_".$tn_width."jpg";
    }

    imagejpeg($tmp,$destination_pic,100);
    imagedestroy($src);
    imagedestroy($tmp);

    if ($unlink_src){
        unlink($source_pic);
    }
}
?>

	<?php
 
						$maxcols = 4; 
						include ("carter.inc");
						$cxn = mysqli_connect($host,$user,$password,$database)
						or die ("couldn't connect to server");
        
						$query = "select * from photos order by photo_date";
						$result = mysqli_query($cxn, $query)
						or die ("Couldn't execute query.");
 
						$count=0; // initialize count
 
						while ($row=mysqli_fetch_assoc($result))
						{
						$count++; // increment count
 
						if ($count == 1)
						 { // initalize table
						?>
 
							<table width="550" border="1" align="center" cellspacing="1" cellpadding="1">
							<tr>
 
						<?php
						 }
						?>
 
							<td align="center">
							<?php
							$source_pic= str_replace("+", " ", $row['url']);
							?>
							<?php
							$tmp = resize_image($source_pic, "auto", 100, 100, true, false);
							?>
							<A HREF="video_player.php?id=<?php echo $id; ?>" target="_blank">
							<IMG SRC="<?php echo tmp; ?>" />
							</a>


						
							</td>
 
						<?php
						if ($count % $maxcols == 0)
						 { // if modulus of count is = 0 then end row
  						echo "</tr><tr>"; 
						 }
 
						}
 
						if ($count)
						{ // data exists
						 $fill = ($count % $maxcols); // current column
 						if ($fill){ // if not last column already fill in blank columns
  						for ($i = $fill; $i <= ($maxcols -1); $i++){
   						echo "<td>&nbsp;</td>";
  						}
  						echo "</tr>";
						 }
						 echo "</table>";
						}
						?>
					</td>
					</tr>
					</table>

Open in new window

The error means php can't find the source image file. In your code you pass variable $source_pic, whose value is
str_replace("+", " ", $row['url']): try to echoe the result of this line of code:

echo str_replace("+", " ", $row['url']);

Keep in mind this should be the exatc path to an image file.

Cheers
OK, margus, I think we're getting closer.

Take a look at my page: http://countryshowdown.com/Texaco/photo_test.php

My code is attached. Where am I blowin' it?
<?php
function resize_image($source_pic, $destination_pic = "auto", $new_width, $new_height, $proprtion = false, $unlink_src = false){
    $src = imagecreatefromjpeg($source_pic);
    list($width,$height) = getimagesize($source_pic);

    $x_ratio = $new_width / $width;
    $y_ratio = $new_height / $height;

    if (proportions){
        if (($x_ratio * $height) < $new_height){
            $tn_height = ceil($x_ratio * $height);
            $tn_width = $new_width;
        }else{
            $tn_width = ceil($y_ratio * $width);
            $tn_height = $new_height;
        }
    }else{
        $tn_width = $new_width;
        $tn_height = $new_height;
    }

    $tmp=imagecreatetruecolor($tn_width,$tn_height);
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

    if ($destination_pic == "auto"){
        $filearr = explode(".", $source_pic);
        $basename = array_shift($filearr);
        $destination_pic = $basename."_".$tn_width."jpg";
    }

    imagejpeg($tmp,$destination_pic,100);
    imagedestroy($src);
    imagedestroy($tmp);

    if ($unlink_src){
        unlink($source_pic);
    }
}
?>

	<?php
 
						$maxcols = 4; 
						include ("carter.inc");
						$cxn = mysqli_connect($host,$user,$password,$database)
						or die ("couldn't connect to server");
        
						$query = "select * from photos order by photo_date";
						$result = mysqli_query($cxn, $query)
						or die ("Couldn't execute query.");
 
						$count=0; // initialize count
 
						while ($row=mysqli_fetch_assoc($result))
						{
						$count++; // increment count
 
						if ($count == 1)
						 { // initalize table
						?>
 
							<table width="550" border="1" align="center" cellspacing="1" cellpadding="1">
							<tr>
 
						<?php
						 }
						?>
 
							<td align="center">
							<?php
							$clean_image= str_replace("+", " ", $row['url']);
							$source_pic = "../Photos/";
							$source_pic .= $clean_image;
							echo $source_pic;
							?>
							<?php
							$tmp = resize_image($source_pic, "auto", 100, 100, true, false);
							?>
							<A HREF="video_player.php?id=<?php echo $id; ?>" target="_blank">
							<IMG SRC="<?php echo tmp; ?>" />
							</a>


						
							</td>
 
						<?php
						if ($count % $maxcols == 0)
						 { // if modulus of count is = 0 then end row
  						echo "</tr><tr>"; 
						 }
 
						}
 
						if ($count)
						{ // data exists
						 $fill = ($count % $maxcols); // current column
 						if ($fill){ // if not last column already fill in blank columns
  						for ($i = $fill; $i <= ($maxcols -1); $i++){
   						echo "<td>&nbsp;</td>";
  						}
  						echo "</tr>";
						 }
						 echo "</table>";
						}
						?>
					</td>
					</tr>
					</table>

Open in new window

One more thing: I just noticed that there are a number of new images sitting in my directory. Does this script actually create new images, or does it simply display them in a resized format? At the end of the day, I just want to display them in a uniform manner, I don't need to alter them. Does that make a difference?
OK, I found a script that I believe provides me exactly what I'm looking for.

I've got the code that includes the function and my anemic attempt to make it happen attached. Here's the thing: I get an error that says: Fatal error: Call to undefined function imageResize() in /home/content/b/r/u/brucegust/html/Texaco/photo_go.php on line 49

Dang it! What am I doing wrong?


 
<?php

//declare your variables

function imgResize($width, $height, $target) {
//takes the larger size of the width and height and applies the formula. Your function is designed to work with any image in any size.
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value

$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you can plug this function inside an image tag so that it will set the image to the correct size, without putting a whole script into the tag.
return "width=\"$width\" height=\"$height\"";

}

?>

<html>
<head>
<title>test</title>
</head>

<body>

<table>
<?php 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");

$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
while($query_row=mysqli_fetch_assoc($result))
{
extract($query_row);
?>
<tr>
<td>
<?php
$mypicture = getimagesize("../Photos/$url");
?>
<img src="../Photos/<?php echo $url; ?>" <?php imageResize($mypicture[0],  $mypicture[1], 100); ?>
</td>
</tr>
<?php
}
?>
</table>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My script effectively create new resized images.

I saw your page: error is raised only with bitmap images: this format is not supported.

Cheers