?
Solved

call a function twice

Posted on 2010-08-17
6
Medium Priority
?
368 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
[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
6 Comments
 
LVL 5

Accepted Solution

by:
ploftin earned 668 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 668 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
Industry Leaders: 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 9

Assisted Solution

by:Snarfles
Snarfles earned 664 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

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!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 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