Solved

How do I get this function to work

Posted on 2011-03-03
12
860 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:brucegust
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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>

0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
0
 

Author Comment

by:brucegust
Comment Utility
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
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
0
 
LVL 7

Expert Comment

by:Vimal DM
Comment Utility
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
0
 

Author Comment

by:brucegust
Comment Utility
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

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
0
 

Author Comment

by:brucegust
Comment Utility
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

0
 

Author Comment

by:brucegust
Comment Utility
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?
0
 

Author Comment

by:brucegust
Comment Utility
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

0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
You are calling imageResize function but function name is imgResize ;-)
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
My script effectively create new resized images.

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

Cheers
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now