Jump to content

function return


levidyllan

Recommended Posts

I have created an image resize code into a function which saves me outputing the code four times as my form has four image inputs.

 

Anyway at then end of this function I have an echo section that displays the image resized ( 4 in all images as function is called 4 times).

 

Anyway I would like to return the image name after the function is run, but not sure how to get this info.

 

The following code is the function:

function resize($name){


 $size = 150; // the thumbnail height 

     $filedir = 'pics/'; // the directory for the original image 
     $thumbdir = 'pics/'; // the directory for the thumbnail image 
     $prefix = 'thumbs_'; // the prefix to be added to the original name 

     $maxfile = '2000000'; 
     $mode = '0666'; 
      
     $userfile_name = $_FILES[$name]['name']; 
     $userfile_tmp = $_FILES[$name]['tmp_name']; 
    	
         $prod_img = $filedir.$userfile_name; 

         $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 
         move_uploaded_file($userfile_tmp, $prod_img); 
         chmod ($prod_img, octdec($mode)); 
          
         $sizes = getimagesize($prod_img); 

         $aspect_ratio = $sizes[1]/$sizes[0]; 

         if ($sizes[1] <= $size) 
         { 
             $new_width = $sizes[0]; 
             $new_height = $sizes[1]; 
         }else{ 
             $new_height = $size; 
             $new_width = abs($new_height/$aspect_ratio); 
         } 

         $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image:' . $name); 
      
     $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image: '. $name); 
     
     ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing: '. $name); 
     
     ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving: '. $name); 
  
         imagedestroy($destimg); 
	 unlink($destimg);//Delete the original image

    echo '<a href="'.$prod_img.'"> ';
        echo '<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> ';
    	echo ' </a><br /><br />';
	 }

 

The follwoing code calls the function:

 if (isset($_FILES['image']['name'])) {	
resize("image");//resize function
    } 

 

Now, after the function is run I would like to some how obtain the image name "$prod_img_thumb" so I can place in a variable to use later etc, how can I return the $prod_img_thumb value from out the function?

 

 

Link to comment
https://forums.phpfreaks.com/topic/61611-function-return/
Share on other sites

all you need to do is use the return operator:

 

function resize(blah)
{
  // blah blah
  // echo image
  return $prod_img_thumb;
}

 

and when you call it, you can assign whatever you want to the returned value:

 

if (isset($_FILES['image']['name']))
{
$prod_img_thumb = resize("image");//resize function
}

Link to comment
https://forums.phpfreaks.com/topic/61611-function-return/#findComment-306668
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.