Make two files resize and save to different folders

I have a form that uploads user data and images to Database and folder on the server. I am resizing the images to be no bigger than 1200px in width. So the first part (This Works start) of the code is working just fine. But the second part where i try to make a second image that is 400px in width and be saved to another folder is not working. Why is this, anybody have an idea please?

Thank you.

//This Works start

$save_th = $uploadfolder_th . "/" . $upload_DstName[$i]; //This is the new file you saving
$save = $uploadfolder . "/" . $upload_DstName[$i]; //This is the new file you saving
$file = $uploadfolder . "/" . $upload_DstName[$i]; //This is the original file

list($width, $height) = getimagesize($file) ;

$modwidth = 1200;

$diff = $width / $modwidth;

$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 80) ;

//This Works end

//--------------------------------------------------------------------------------------------------

//This is not Working start

$modwidth_th = 400;

$diff_th = $width / $modwidth_th;

$modheight_th = $height / $diff_th;
$tn_th = imagecreatetruecolor($modwidth_th, $modheight_th) ;
$image_th = imagecreatefromjpeg($file) ;
imagecopyresampled($tn_th, $image_th, 0, 0, 0, 0, $modwidth_th, $modheight_th, $width, $height) ;

imagejpeg($tn_th, $save_th, 80) ;

//This is not Working end

I have created and validated the following script but beware the created images require folder write permissions.


Try this:

<?php 
	declare(strict_types=1);
	error_reporting(-1);
	ini_set('display_errors', 'true');

// FILE NAME 
	$file = 'douglas-adams-happy-towel-day.jpg';

	if( file_exists($file)):
		makeThumb($file, 'thumb-42.jpg', 42);
		makeThumb($file, 'thumb-88.jpg', 88);
	else:
		echo 'Whoops, cannot find: ' .$file;;
	endif;	


//====================================================================
function makeThumb
(
	string $file 	 = '',
	string $thumb	 = 'thumb.jpg',
	int $modwidth_th = 400
)
:bool // return FALSE not used
{
	list($width, $height) = getimagesize($file) ;	
	
	$diff_th 	  = $width / $modwidth_th;
	$modheight_th = $height / $diff_th;
	$modheight_th = (int) $modheight_th;
	$tn_th 	 	  = imagecreatetruecolor($modwidth_th, $modheight_th) ;
	$image_th 	  = imagecreatefromjpeg($file) ;
	$ok 		  = imagecopyresampled
	(
		$tn_th, 
		$image_th, 
		0, 0, 0, 0, 
		$modwidth_th, 
		$modheight_th, 
		$width, 
		$height
	) ;
	$ok   = imagejpeg($tn_th, $thumb, 80) ;
	$dims = getimagesize($thumb);

	echo '<pre>' .print_r($dims, true) .'</pre>';
	echo '<img src="' .$thumb .'" alt="#" />';

	return FALSE;
}//

Hello, i will test this later to day. Thank you for your help.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.