Solved

call a function twice

Posted on 2010-08-17
6
356 Views
Last Modified: 2012-05-10
how can I make this a function
and call the function




I want to call the function twice
so the image resizes twice
<?php

// This is the temporary file created by PHP
//$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$uploadedfile="uploaded/7dkitt2.jpg";

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
//$filename = "images/". $_FILES['uploadfile']['name'];
$filename = "uploaded/newfile.jpg";
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.
?>

Open in new window

0
Comment
Question by:rgb192
6 Comments
 
LVL 5

Accepted Solution

by:
ploftin earned 167 total points
ID: 33460136
Try this.
<?php

function resizeImage($uploadedfile, $newfile) {
	// This is the temporary file created by PHP
	
	// Create an Image from it so we can do the resize
	$src = imagecreatefromjpeg($uploadedfile);
	
	// Capture the original size of the uploaded image
	list($width,$height)=getimagesize($uploadedfile);
	
	// For our purposes, I have resized the image to be
	// 600 pixels wide, and maintain the original aspect
	// ratio. This prevents the image from being "stretched"
	// or "squashed". If you prefer some max width other than
	// 600, simply change the $newwidth variable
	$newwidth=600;
	$newheight=($height/$width)*600;
	$tmp=imagecreatetruecolor($newwidth,$newheight);

	// this line actually does the image resizing, copying from the original
	// image into the $tmp image
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	
	// now write the resized image to disk. I have assumed that you want the
	// resized, uploaded image file to reside in the ./images subdirectory.
	//$filename = "images/". $_FILES['uploadfile']['name'];
	imagejpeg($tmp,$newfile,100);
	
	imagedestroy($src);
	imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
	// has completed.
}

//Call the function like this:
resizeImage("uploaded/7dkitt2.jpg", "uploaded/newfile.jpg")

?>

Open in new window

0
 
LVL 8

Assisted Solution

by:ropenner
ropenner earned 167 total points
ID: 33460152
It is not exactly clear which portion of the code you want to call twice, so here is a general idea.

$param1 = somecode
somemorecode

somecode_I_want_to_repeat
$some_new_value = somemorecoee_I_want_to_repeat($param1)

code_after_initial_code.

Now you want the repeatable code to be in a function .. so it becomes

$param1 = somecode
somemorecode

// call function twice
$output = new_function($param1);
$second_output = new_function($output);

function new_function($param1) {
     somecode_I_want_to_repeat
    $some_new_value = somemorecoee_I_want_to_repeat($param1)
     return $some_new_value;
}

code_after_initial_code.
0
 
LVL 5

Expert Comment

by:ploftin
ID: 33460211
I didn't state it but if you wanted to call m function a second time, add the following to line 37.
resizeImage("uploaded/newfile.jpg", "uploaded/newfile.jpg")

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 166 total points
ID: 33460582
Rather than adding two lines calling the same function like so which is messy...

resizeImage("uploaded/newfile.jpg", "uploaded/newfile.jpg");
resizeImage("uploaded/newfile.jpg", "uploaded/newfile.jpg");

perhaps try this



(extending ploftin's code)
<?php

function resizeImage($uploadedfile, $newfile, $iterations) {
	for ( $counter = 1; $counter <= $iterations; $counter++) { //this is what loops through the code twice or more
        // This is the temporary file created by PHP
        
        // Create an Image from it so we can do the resize
        $src = imagecreatefromjpeg($uploadedfile);
        
        // Capture the original size of the uploaded image
        list($width,$height)=getimagesize($uploadedfile);
        
        // For our purposes, I have resized the image to be
        // 600 pixels wide, and maintain the original aspect
        // ratio. This prevents the image from being "stretched"
        // or "squashed". If you prefer some max width other than
        // 600, simply change the $newwidth variable
        $newwidth=600;
        $newheight=($height/$width)*600;
        $tmp=imagecreatetruecolor($newwidth,$newheight);

        // this line actually does the image resizing, copying from the original
        // image into the $tmp image
        imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
        
        // now write the resized image to disk. I have assumed that you want the
        // resized, uploaded image file to reside in the ./images subdirectory.
        //$filename = "images/". $_FILES['uploadfile']['name'];
        imagejpeg($tmp,$newfile,100);
        
        imagedestroy($src);
        imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
        // has completed.
	}
}

//Call the function like this:
resizeImage("uploaded/7dkitt2.jpg", "uploaded/newfile.jpg", 2);

?>

Open in new window

0
 
LVL 5

Expert Comment

by:ploftin
ID: 33460728
Nice addition Snarfles.
0
 

Author Closing Comment

by:rgb192
ID: 33461073
thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

809 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