Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I get this function to work

Posted on 2011-03-03
12
Medium Priority
?
876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35031539
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 31

Expert Comment

by:Marco Gasi
ID: 35031617
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
ID: 35031875
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35033881
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
ID: 35034110
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
ID: 35039171
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35039318
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
ID: 35039455
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
ID: 35039561
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
ID: 35040082
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 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 35040117
You are calling imageResize function but function name is imgResize ;-)
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35040144
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…

705 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