[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how can i resize image and keep the aspect ratio ?

Posted on 2009-05-12
9
Medium Priority
?
902 Views
Last Modified: 2012-05-06
Hello here is my script used to upload an image so i need to have the size of the image to 150px
note that i am using easyphp that have php 4.0 so i look for a solution that work on it.
PS: please help by adding the resize image to my code structure listed below
function upload($file,$t,$path)
{
	$post=$_FILES[$file];
	$title=$_POST[$t];
	$rand=rand(100,10000);
	//upload setting
	//$path="../images/products/";	
	//rename
	$remove_these = array(' ','`','"','\'','\\','/');
	//Make the filename unique
	$type1=explode("/" ,$post['type']);
	
		if ( $post['size'] > 0 ) {
		
			if ( $post['type'] != "image/pjpeg" && $post['type'] != "image/gif" && $post['type'] != "image/bmp" && $post['type'] != "image/jpg"  ){
			$picture='1';
			}
			else{
				if ( $post['type'] == "image/pjpeg"){ $picture= $title."-".$rand.'.jpg'; }
				else{ $picture=$title."-".$rand.".".$type1[1];}
					
					if(move_uploaded_file($post["tmp_name"], $path.$picture ) ) { 
						chmod($path.$picture, 0777);
						$my_new_file = $path.$picture;
						} else{ $test="";  }
				}
		
 
		}
		else {
		$picture='0';
		}
return $picture;
}
 
 
//here the trick to render the message to the notification div
$print=upload("image","title","../images/sell/products/");
$script='<script type="text/javascript">';
if ( $print == "0" )
{
$script.='parent.document.getElementById("nimage").innerHTML="KINDLY CHOOSE A VALID IMAGE";'. "\n"; 
$script.='parent.document.getElementById("dimage").style.backgroundColor="#FF4200";'. "\n"; 
}
if ( $print == "1" )
{
$script.='parent.document.getElementById("nimage").innerHTML="NOT AN IMAGE TYPE! | ONLY JPEG, GIF AND BMP IMAGE ACCEPTED";'. "\n"; 
$script.='parent.document.getElementById("dimage").style.backgroundColor="#FF4200";'. "\n"; 
}
else
{
$script.='parent.document.getElementById("nimage").innerHTML="IMAGE SUCCESSFULLY UPLOADED";'. "\n"; 
$script.='parent.document.getElementById("dimage").style.backgroundColor="#ABCDEC";'. "\n"; 
$_SESSION['sellimage']=$print;
 
}
$script.='</script>';
 
echo $script;

Open in new window

0
Comment
Question by:Styleminds
[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
  • 5
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
Sander Stad earned 2000 total points
ID: 24363864
This shoud help you:

function upload($file,$t,$path){
 
	$post=$_FILES[$file];
	$title=$_POST[$t];
	$rand=rand(100,10000);
	//upload setting
	//$path="../images/products/";  
	
	//rename
	$remove_these = array(' ','`','"','\'','\\','/');
	
	//Make the filename unique
	$type1=explode("/" ,$post['type']);
 
	if( $post['size'] > 0 ) {
		if ( $post['type'] != "image/pjpeg" && $post['type'] != "image/gif" && $post['type'] != "image/bmp" && $post['type'] != "image/jpg"  ){
			$picture='1';
		} else{
			if ( $post['type'] == "image/pjpeg"){ 
				$picture= $title."-".$rand.'.jpg'; 
			}else{ $picture=$title."-".$rand.".".$type1[1];}
		
			if(move_uploaded_file($post["tmp_name"], $path.$picture ) ) { 
				chmod($path.$picture, 0777);
				$my_new_file = $path.$picture;
				
				//get height and width from image
				$dimensions = getimagesize($my_new_file); 
				$sourceWidth = $dimensions[0]; 
				$sourceHeight = $dimensions[1];
				
				// set the longest side
				$longSide = $sourceHeight
	
				// If the longest side is smaller than the width
				if(sourceHeight < $sourceWidth){
					$longSide = $oldImage.Width
				}
				
				// if the longest side is greater than the new size
				if($longSide > 150) {
					//if the height is smaller than the width
					if($sourceHeight < $sourceWidth) {
						$newH = 150
						$newW = ($sourceWidth * 150 / $sourceHeight)
					# Als de hoogte groter is dan de breedte
					} else {
						$newW = 150
						$newH = ($sourceHeight * $newW / $sourceWidth)
					}
		
					// read the image
					$image = imagecreatefromjpeg($my_new_file);
    
					// create a new image width the desired size
					$destination = imagecreatetruecolor($newW, $newH);
    
					// fill the new image with the resized image
					imagecopyresampled($destination, $image, 0, 0, 0, 0, $newW, $newH, $sourceWidth, $sourceHeight);
    
					// display the image
					imagejpeg($destination);
    
		
				} else {
					// do nothing
				}
			} else{ 
				$test="";  
			}
		}
	}else {
		$picture='0';
	}
	
	return $destination;
	
	// destroy the sourcedata
	imagedestroy($image);
 
	// destroy the destination data
	imagedestroy($destination); 
}
 
 
//here the trick to render the message to the notification div
$print=upload("image","title","../images/sell/products/");
$script='<script type="text/javascript">';
if ( $print == "0" )
{
$script.='parent.document.getElementById("nimage").innerHTML="KINDLY CHOOSE A VALID IMAGE";'. "\n"; 
$script.='parent.document.getElementById("dimage").style.backgroundColor="#FF4200";'. "\n"; 
}
if ( $print == "1" )
{
$script.='parent.document.getElementById("nimage").innerHTML="NOT AN IMAGE TYPE! | ONLY JPEG, GIF AND BMP IMAGE ACCEPTED";'. "\n"; 
$script.='parent.document.getElementById("dimage").style.backgroundColor="#FF4200";'. "\n"; 
}
else
{
$script.='parent.document.getElementById("nimage").innerHTML="IMAGE SUCCESSFULLY UPLOADED";'. "\n"; 
$script.='parent.document.getElementById("dimage").style.backgroundColor="#ABCDEC";'. "\n"; 
$_SESSION['sellimage']=$print;
 
}
$script.='</script>';
 
echo $script;

Open in new window

0
 

Author Comment

by:Styleminds
ID: 24364976
there many miss type in this script i try to fix but however there still some question mark on this ! where these variable are not delcared anywhere in the function

                                if($sourceHeight < $sourceWidth){
                                        $longSide = $oldImage.Width;
                                }

and the function imagecreatefromjpeg() give this error
Fatal error: Call to undefined function: imagecreatefromjpeg() in i:\easyphp1-8\www\audiominds\newmodules\uploadimage.php on line 83

so please advice
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24371904
Sorry made a little typo:

if($sourceHeight < $sourceWidth){
           $longSide = $sourceWidth;
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Styleminds
ID: 24372851
this function return that is undefined !
imagecreatefromjpeg($my_new_file);
how can i fix it ?
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24373135
Assuming you're on Windows:
 Open a text editor (notepad, etc.).
 Open php.ini.
 Look for a line like this:
 ;extension=php_gd.dll
 Erase the semicolon (;).
0
 

Author Comment

by:Styleminds
ID: 24375028
there is only
extension=php_gd2.dll
and it didn't have any semocolon!
any idea ! how can i get this function to work !  i just need to remind what i advice above i am using easy php with php4 so i need a solution for this version i don't want to upgrade for the time being.
Thanks.
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24382051
can you get the phpinfo. do you see anything like GD in there?



<?php
 
phpinfo();
 
?>

Open in new window

0
 

Author Comment

by:Styleminds
ID: 24382657
sorry i get lost ! where i can find this also in php.ini? does this available in php folder as i found another one in the apashe server
please advise.
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24382677
no create a php file and paste the above code in it.
run the file and you'll get all the modules and other information about your current php configuration.
0

Featured Post

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.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

656 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