Jump to content

[SOLVED] Get a varable to loop out side the loop


redarrow

Recommended Posts

advance thank you.

 

i need to get the varable to loop out side the loop but the

condition set within the loop so the varable loop's outside.

 

can it be done cheers

 

 

<?php

// set a message
$word="my name is redarrow and i love php";

//set the loop
for($i=0; $i<5; $i++){

//add a varable to the loop to echo
$result=$word;
}
//see the looped result. but this wont loop ok,so how can it looped outside but set within the loop.
echo "<br>$result<br>";
?>

Link to comment
Share on other sites

 

there you go a upload script suppose to put three diffrent size images to three folders and the defult folder

 

so it is.

 

folder

pics

pics1

pics2

pics3

 

 

the problam is that a loop wont work unless you no a around it using the imageresample syntex ok

 

thanks for helping.

 

<?php

if(isset($_POST['Submit']))

{

    $size[1] ='100';
    $size[2] ='200'; 
    $size[3] ='300';
    
    $filedir = 'pics/'; 


 $thumbdir[1]='pics1/';
 $thumbdir[2]='pics2/';
 $thumbdir[3]='pics3/';

$prefix = '00005'; 

$maxfile = '2000000';

$mode = '0666';

$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];

if (isset($_FILES['image']['name'])) 
{

$prod_img = $filedir.$userfile_name;

$prod_img_thumb[1] = $thumbdir[1].$prefix.$userfile_name;
$prod_img_thumb[2] = $thumbdir[2].$prefix.$userfile_name;
$prod_img_thumb[3] = $thumbdir[3].$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[1]) && ($sizes[1] <= $size[2]) && ($sizes[1] <= $size[3]))
	{
		$new_width = $sizes[0];
		$new_height = $sizes[1];

	}else{

		$new_height[1] = $size[1];
		$new_height[2] = $size[2];
		$new_height[3] = $size[3];

		$new_width[1] = abs($new_height[1]/$aspect_ratio);
		$new_width[2] = abs($new_height[2]/$aspect_ratio);
		$new_width[3] = abs($new_height[3]/$aspect_ratio);
	}
		$destimg=ImageCreateTrueColor($new_width[1],$new_height[1])
		or die('Problem In Creating image')&& ImageCreateTrueColor($new_width[2],$new_height[2])
		or die('Problem In Creating image')&& ImageCreateTrueColor($new_width[3],$new_height[3])
		or die('Problem In Creating image');

	$srcimg=ImageCreateFromJPEG($prod_img)
		or die('Problem In opening Source Image');
	if(function_exists('imagecopyresampled'))
	{

imagecopyresampled($destimg,$srcimg,0,0,0,0,($new_width[1])&&($new_width[2])&&($new_width[3]),($new_height[1])&&($new_height[2])&&($new_height[3]),ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}else{
		Imagecopyresized($destimg,$srcimg,0,0,0,0,($new_width[1])&&($new_width[2])&&($new_width[3]),($new_height[1])&&($new_height[2])&&($new_height[3]),ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}
	ImageJPEG($destimg,($prod_img_thumb[1])&&($prod_img_thumb[2])&&($prod_img_thumb[3]),90)
		or die('Problem In saving');
	imagedestroy($destimg);

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

}else{

echo '
<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
<input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

Link to comment
Share on other sites

here the new version still no luck i got it all going but only worked while 1 array was chosen

but as you can see if you add any loop anywere it upsets the whole code.

 

current code but dosent work the defualt picture goes in the pic folder but the other three folders pic1 pic2 pic3

dont get there new made pics with selected sizes as stated in the array.

 

there are no error's so i guess the three new images using the array numbers and the && are not working becouse

of the fact i want three diffrent pics made from the imageresample statement.

 

so who going to crack this then lol(guru were lol).

 

This code should be able to post a pic in one folder and then use gd to create three other pics and put them in

the correct folders and create there specified sizes.

 

<?php

if(isset($_POST['Submit']))

{

    $size[1] ='100';
    $size[2] ='200'; 
    $size[3] ='300';
    
    $filedir = 'pics/'; 


 $thumbdir[1]='pics1/';
 $thumbdir[2]='pics2/';
 $thumbdir[3]='pics3/';

$prefix = '00005'; 

$maxfile = '2000000';

$mode = '0666';

$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];

if (isset($_FILES['image']['name'])) 
{

$prod_img = $filedir.$userfile_name;

$prod_img_thumb[1] = $thumbdir[1].$prefix.$userfile_name;
$prod_img_thumb[2] = $thumbdir[2].$prefix.$userfile_name;
$prod_img_thumb[3] = $thumbdir[3].$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[1]) && ($sizes[1] <= $size[2]) 
&& ($sizes[1] <= $size[3]))
	{
		$new_width = $sizes[0];
		$new_height = $sizes[1];

	}else{

		$new_height[1] = $size[1];
		$new_height[2] = $size[2];
		$new_height[3] = $size[3];

		$new_width[1] = abs($new_height[1]/$aspect_ratio);
		$new_width[2] = abs($new_height[2]/$aspect_ratio);
		$new_width[3] = abs($new_height[3]/$aspect_ratio);
	}
		$destimg=ImageCreateTrueColor(($new_width[1])&&($new_width
[2])&&($new_width[3]),($new_height[1])&&($new_height[2])&&($new_height[3]))
		or die('Problem In Creating image');

	$srcimg=ImageCreateFromJPEG($prod_img)
		or die('Problem In opening Source Image');
	if(function_exists('imagecopyresampled'))
	{

imagecopyresampled($destimg,$srcimg,0,0,0,0,($new_width[1])&&($new_width[2])&&($new_width[
3]),($new_height[1])&&($new_height[2])&&($new_height[3]),ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}else{
		Imagecopyresized($destimg,$srcimg,0,0,0,0,($new_width[1])&&($new_width
[2])&&($new_width[3]),($new_height[1])&&($new_height[2])&&($new_height[3]),ImageSX($srcim
g),ImageSY($srcimg))
		or die('Problem In resizing');
	}
	ImageJPEG($destimg,($prod_img_thumb[1])&&($prod_img_thumb[2])&&($prod_img_
thumb[3]),90)
		or die('Problem In saving');
	imagedestroy($destimg);

}
echo '
<a href="'.$prod_img.'">
	<img src="'.$prod_img_thumb[3].'" width="'.$new_width[3].'" heigt="'.$new_h
eight[3].'">
</a>';

}else{

echo '
<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multip
art/form-data">
<input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

Link to comment
Share on other sites

You are passing invalid parameters to functions. You cannot pass this lot as a parameter:

($new_width[1])&&($new_width[2])&&($new_width[3])

 

What you are doing there is a comparison.

 

In order to achieve what you are trying to do you have to run through the following functions ImageCreateTrueColor, ImageCreateFromJPEG, imagecopyresampled, Imagecopyresized, ImageJPEG, and imagedestroy  for every size image you want to create from the original image.

 

Here is a good way of doing it:

<?php

// this our dedicated function that creates our resized images
function createImage($width, $height, $srcimg, $img_thumb)
{
    $srcImg = ImageCreateTrueColor( $width, $height ) or die('Problem In Creating image');

    if( function_exists('imagecopyresampled' ))
    {
        imagecopyresampled( $srcImg, $srcimg, 0, 0, 0, 0, $width, $height, ImageSX($srcimg), ImageSY($srcimg) ) or die('Problem In resizing img #' . $key);
    }
    else
    {
        Imagecopyresized( $srcImg, $srcimg, 0, 0, 0, 0, $width, $height, ImageSX($srcimg), ImageSY($srcimg) ) or die('Problem In resizing img #' . $key);
    }

    ImageJPEG( $srcImg, $img_thumb, 90 ) or die('Problem In saving img');

    imagedestroy( $srcImg );
}

if(isset($_POST['Submit']))
{
    $size[1] = 100;
    $size[2] = 200;
    $size[3] = 300;

    $filedir = './pics/';

    $thumbdir[1] = './pics1/';
    $thumbdir[2] = './pics2/';
    $thumbdir[3] = './pics3/';

    $prefix = '00005';
    $maxfile = '2000000';
    $mode = '0666';

    $userfile_name = $_FILES['image']['name'];
    $userfile_tmp  = $_FILES['image']['tmp_name'];
    $userfile_size = $_FILES['image']['size'];
    $userfile_type = $_FILES['image']['type'];

    if (isset($_FILES['image']['name']))
    {
        $prod_img = $filedir.$userfile_name;

        $prod_img_thumb[1] = $thumbdir[1].$prefix.$userfile_name;
        $prod_img_thumb[2] = $thumbdir[2].$prefix.$userfile_name;
        $prod_img_thumb[3] = $thumbdir[3].$prefix.$userfile_name;

        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        $srcimg = ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');

        $sizes = getimagesize($prod_img);

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

        if (($sizes[1] <= $size[1]) || ($sizes[1] <= $size[2]) || ($sizes[1] <= $size[3]))
        {
            $new_width = $sizes[0];
            $new_height = $sizes[1];
        }
        else
        {
            $new_height[1] = $size[1];
            $new_height[2] = $size[2];
            $new_height[3] = $size[3];

            $new_width[1] = abs($new_height[1]/$aspect_ratio);
            $new_width[2] = abs($new_height[2]/$aspect_ratio);
            $new_width[3] = abs($new_height[3]/$aspect_ratio);
        }

        // loop through each img width and call a dedicated function to
        // create different sizes of the uploaded image
        foreach($new_width as $cur_img => $cur_img_width)
        {
            // this function uses the variables set above
            createImage($cur_img_width, $new_height[$cur_img], $srcimg, $prod_img_thumb[$cur_img]);
        }
    }

    /*echo '<a href="'.$prod_img.'">
    <img src="'.$prod_img_thumb[3].'" width="'.$new_width[3].'" height="'.$new_height[3].'">
</a>';*/

// show the created images
    foreach($prod_img_thumb as $cur_img => $img_thumb)
    {
        echo '<h3>Img #' . $cur_img . '</h3>
<a href="' . $prod_img . '">
    <img src="'.$prod_img_thumb[$cur_img].'" width="'.$new_width[$cur_img].'" height="'.$new_height[$cur_img].'">
</a><br />
Dimensions: ' . $new_width[$cur_img] . 'px X ' . $new_height[$cur_img] . 'px';
    }
}
else
{
    echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
    <input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

Link to comment
Share on other sites

wildteen your grate, I wanted to learn what you just done such advance work,

your such a good progammer well done m8.

 

if possable wildteen if you get time can you brake the code even more down yes i do understand the code but i am going to try and rewrite it on my own, also learn the process verry well.

 

 

once agin thank you.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.