Jump to content

Recommended Posts

I have two questions, what am I doing wrong? my script isnt working. It doesnt return errors nor a rezised pic. My second questions is just asking for ideas of how to improve this script in terms of security and quality. any help is appreciated. 

<?php
#TODO: find ways to improve quality
	$quality = '100'; 
	//in imagejpeg() type function default quality = 75, min = 0, max 100
	$maxW = '180';
	$maxH = '180';

	$dirLoad = 'photos/load/'; //thumb size dir
	$dirOriginal = 'photos/';  //original size dir
	
	if(isset($_GET['a']) AND isset($_GET['y']) AND isset($_GET['i'])){
		
		$falbum = stripslashes($_GET['a']);
		$fyear = stripslashes($_GET['y']);	
		$img = stripslashes($_GET['i']);
	} else {
		echo 'lol';
		exit;
	}
	
	$locOriginal = $dirOriginal.$fyear.'/'.$falbum.'/'.$img; //get normal  location of img
	$locSave = $dirLoad.$fyear.'_'.$falbum.'_'.$img; // location and how img will be save
	
#TODO: maybe redefine algorith to use crop to add more pic quality	
list($originalW, $originalH) = getimagesize($locOriginal);
$ratio = $originalW / $originalH;

if($ratio > 1){ // landscape shape original photo
	$tgtH = $maxW/$ratio;  
	$tgtW = $maxW;
} else { // portrait shape Original photo
	$tgtW = $maxH*$ratio;
	$tgtH = $maxH;
}

//echo $originalW.'x'.$originalH.'@'.$ratio; #checking code behavior
//echo '<br>'.$tgtW.'x'.$tgtH;   			 #checking code behavior

//get file extension 	
$ext = explode('.',$locOriginal);
$ext = end($ext); //only variables should be passed by ref with end
$ext = strtolower($ext); //turn it into lower case to match with switch below, just in case

	switch($ext){ //open the image according to file extension
	#note: must image we will work with are jpg/jpeg extension, added switch just in case 
	#also bmt files are not supported by this script
    	case 'jpg':  //$imgOld = @imagecreatefromjpeg($locOriginal); break; no need but makes it look uniform
        case 'jpeg': $imgOld = @imagecreatefromjpeg($locOriginal); break;
        case 'gif':  $imgOld = @imagecreatefromgif($locOriginal); break;
        case 'png':  $imgOld = @imagecreatefrompng($locOriginal); break;
        default:      $imgOld = false; break;
    }
	return $imgOld;
	
	//create a blank img with target dim.
	$imgNew = imagecreatetruecolor($tgtW, $tgtH); 
	
	//resample old img with new imgNew size [0,0,0,0] crop options
	ImageCopyResampled($imgNew,$imgOld, 0, 0, 0, 0, $tgtW, $tgtH,$originalH, $originalH); 

	switch($ext){ // #edit added header, forgot to
	#also bmt files are not supported by this script
    	case 'jpg':
        case 'jpeg': Header("Content-type: image/jpeg"); Imagejpeg($imgNew,$locSave,$quality); break;
        case 'gif': Header("Content-type: image/gif"); Imagegif($imgNew,$locSave,$quality); break;
        case 'png': Header("Content-type: image/png"); Imagepng($imgNew,$locSave,$quality); break;
        default: false; break;
    }


ImageDestroy($imgOld);
ImageDestroy($imgNew);

	
?>
Edited by grasshopper31
Link to comment
https://forums.phpfreaks.com/topic/285831-php-image-resize-help/
Share on other sites

you need to remove all the @ error suppressors from the code and set php's error_reporting to E_ALL and display_errors to ON so that any php detected errors will be displayed.

 

you should also temporarily comment out the header() statements if you are trying to display the images in the browser (so that you can see any php error messages) and remove the header() statements if your goal is to save the images to files.

i want to return the img in the browser so that it can be called with img html tag, and also save the re-sized picture in a folder to save time loading the page . note that i added echo 'is on'; that is not showing up neither which should mean the problem is above that?

<?php
#TODO: find ways to improve quality
#bmt files are not supported by this script

	$quality = '100'; 
	//in imagejpeg() type function default quality = 75, min = 0, max 100
	$maxW = '180';
	$maxH = '180';

	$dirLoad = 'photos/load/'; //thumb size dir
	$dirOriginal = 'photos/';  //original size dir
	
	if(isset($_GET['a']) AND isset($_GET['y']) AND isset($_GET['i'])){
		
		$falbum = stripslashes($_GET['a']);
		$fyear = stripslashes($_GET['y']);	
		$img = stripslashes($_GET['i']);
	} else {
		echo 'lol';
		exit;
	}
	
	$locOriginal = $dirOriginal.$fyear.'/'.$falbum.'/'.$img; //get normal  location of img
	$locSave = $dirLoad.$fyear.'_'.$falbum.'_'.$img; // location and how img will be save

#TODO: maybe redefine algorith to use crop to add more pic quality	
list($originalW, $originalH) = getimagesize($locOriginal);
$ratio = $originalW / $originalH;

if($ratio > 1){ // landscape shape original photo
	$tgtH = $maxW/$ratio;  
	$tgtW = $maxW;
} else { // portrait shape Original photo
	$tgtW = $maxH*$ratio;
	$tgtH = $maxH;
}

//echo $originalW.'x'.$originalH.'@'.$ratio; #checking code behavior
//echo '<br>'.$tgtW.'x'.$tgtH;   			 #checking code behavior

//get file extension 	explode replace eregi 
$ext = explode('.',$locOriginal);
$ext = end($ext); //only variables should be passed by ref with end
$ext = strtolower($ext); //turn it into lower case to match with switch below, just in case

 if(!file_exists($locOriginal)){
  echo 'Error Found: File doesnt exist';
  exit;
 }	
	switch($ext){ //open the image according to file extension
	#note: must image we will work with are jpg/jpeg extension, added switch just in case 
    	case 'jpg':  $imgOld = imagecreatefromjpeg($locOriginal); break; //no need but makes it look uniform
        case 'jpeg': $imgOld = imagecreatefromjpeg($locOriginal); break;
        case 'gif':  $imgOld = imagecreatefromgif($locOriginal); break;
        case 'png':  $imgOld = imagecreatefrompng($locOriginal); break;
        default:    echo  'Error Found: Wrong File extension or file does not exist.'; break; //if doesnt match file extension
    }
	
	
	return $imgOld;
	echo 'is on'; ################################
	//create a blank img with target dim.
	$imgNew = imagecreatetruecolor($tgtW, $tgtH); 
	
	//resample old img with new imgNew size [0,0,0,0] crop options
	ImageCopyResampled($imgNew,$imgOld, 0, 0, 0, 0, $tgtW, $tgtH,$originalH, $originalH); 

	switch($ext){ // 
    	case 'jpg':
        case 'jpeg': 
			#Header("Content-type: image/jpeg"); 
			Imagejpeg($imgNew,NULL,$quality); //set to null, to show picture in browser
			Imagejpeg($imgNew,$locSave,$quality); break; //added save location to save the file for future
        case 'gif':  
			#Header("Content-type: image/gif"); 
			Imagejpeg($imgNew,NULL,$quality);
			Imagegif($imgNew,$locSave,$quality); break;
        case 'png':  
			#Header("Content-type: image/png");  
			Imagejpeg($imgNew,NULL,$quality);
			Imagepng($imgNew,$locSave,$quality); break;
        default: false; break;
    }


ImageDestroy($imgOld);
ImageDestroy($imgNew);

	
?>

so the error is happening where return $imgOld; i know this because i switch from:

	return $imgOld;
	echo 'is on'; ################################

to: 


echo 'is on'; ###############################
return $imgOld;

and echo worked.. im using php developmental, so it will show all errors if there is any, my problem is that i dont see nor errors its just plain blank

Edited by grasshopper31

you apparently copied some of this code from inside a function definition, where the return statement was returning a value the function produced to the calling code.

 

you would probably want to remove the return instruction since the code is now in-line where it is being used instead of being inside of a function.

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.