Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Switch

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I have a form which uploads an image and works fine. Now, I want to have a way to change the image format on-the-fly and have added radio buttons to the form, but I am having trouble getting the data into the form. It part, the image input is:

Code:
if ($source = $_FILES["Image"]["tmp_name"]) {
	if (file_exists($source)) {
		$size = @GetImageSize($source);
		$type = "";
			if ($size[0] < 0 || $size[1] < 0) {
				echo "Bad image format!";
			} elseif switch ($_POST("ChangeFormat")) {
				case 1:
				$type = "gif";
				break;
				case 2:
				$type = "jpeg";
				break;
				case 3:
				$type = "png";
				break;
			} else switch ($size[2]) {
				case 1:
				$type = "gif";
				break;
				case 2:
				$type = "jpeg";
				break;
				case 3:
				$type = "png";
				break;
				default:
				echo "Only JPG, GIF or PNG format images may be used";
			}

// some GD image formatting function are were

	}	
}

My main question is to do with switch, which I have used only through cut-and-paste in the past. It was working until I added the elseif part of the conditional and now gives an "unexpected T_SWITCH" error. Can anyone advise on how to use switch properly to do what I need or am I going about it all wrong?

Don

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Wow. You need to stand back and check what you actually want to achieve and what you are doing. In php manual you can check the structure of if-elseif-else structure to further your knowledge. In short, elseif expects a conditional expression to follow in parenthesis and then list of commands to do if expression returns true. In your case you have no expression, you simply try to impose switch statement on php. PHP then gets confused (as am I). Very similar is your else clause, immediately followed by switch. This is improper syntax and will fail in PHP. Can you tell us in English or pseudo code what you're trying to do?
 
The else part of the conditional was already there and working; I added the elseif by copying the idea from the else, which is what didn't work.

It's still not clear why switch works in the else (the code came from an example on php.net) but won't work in the elseif. However, I just figured out the main problem which gets past the error:

Code:
if ($source = $_FILES["Image"]["tmp_name"]) {
    if (file_exists($source)) {
        $size = @GetImageSize($source);
        $type = "";
            if ($size[0] < 0 || $size[1] < 0) {
                echo "Bad image format!";
            } elseif ($_POST("ChangeFormat")) {
                 switch ($_POST("ChangeFormat")) {
                    case 1:
                    $type = "gif";
                    break;
                    case 2:
                    $type = "jpeg";
                    break;
                    case 3:
                    $type = "png";
                    break;
                 }
            } else switch ($size[2]) {
                case 1:
                $type = "gif";
                break;
                case 2:
                $type = "jpeg";
                break;
                case 3:
                $type = "png";
                break;
                default:
                echo "Only JPG, GIF or PNG format images may be used";
            }

// some GD image formatting function are were

    }    
}

The $type variable is being used in the GD coding to create an image and the way it was originally, it created it the same format as the input image. I am trying to feed a value to change the format on the fly but that's another issue.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Is this the code you really have in your script:
Code:
            } elseif ($_POST("ChangeFormat")) {
                 switch ($_POST("ChangeFormat")) {
If it is, shouldn't it read:
Code:
            } elseif ($_POST[b][red][[/red][/b]"ChangeFormat"[b][red]][/red][/b]) {
                 switch ($_POST[b][red][[/red][/b]"ChangeFormat"[b][red]][/red][/b]) {
?

Actually, you might be able to change your code so you don't have to use the switch statements at all:
Code:
<?php
$formats = array('','gif','jpeg','png');
if ($source = $_FILES["Image"]["tmp_name"]) {
    if (file_exists($source)) {
        $size = @GetImageSize($source);
        $type = "";
        if ($size[0] < 0 || $size[1] < 0) {
            echo "Bad image format!";
            continue; }
        if (isset($_POST['ChangeFormat']) &&
              ($_POST['ChangeFormat'] > 0 && $_POST['ChangeFormat'] < 4)) $type = $format[$_POST['ChangeFormat']];
        else if ($size[2] > 0 && $size[2] < 4) $type = $formats[$size[2]];
             else echo 'Only JPG, GIF or PNG format images may be used';
     }

The above code was not checked for syntax errors. YMMV

Ken
 
Thanks, no, it's not what I actually have in my script. I am using a built-in function specific to the IDE application I usually use and rarely, if ever, use the "real" $_POST[]. You're right, of course - it was a typo in the simplified-for-the-real-word sample. Sorry for the confusion.

Thanks also for the isset() idea. I've not used it before and didn't think of it but it certainly looks cleaner than what I have. I'll give it a try once I get it working as it is - it's ALMOST there and is creating the GIF (or whatever I select) thumbnail now.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top