23.13 mobile camera to image fields not working

Please report bugs and any annoyances here. Kindly include all possible details: steps to reproduce, expected result, actual result, screenshots, ... etc.
User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1944
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: 23.13 mobile camera to image fields not working

Post by jsetzer » 2024-04-23 01:32

Although I don't need this specific feature, I did some research. If someone is interested in a solution, check out the PHP function imagecopyresampled for resizing images:
https://www.php.net/manual/en/function. ... ampled.php

TABLENAME_after_insert and TABLENAME_after_update hooks should be a good place.

It may be useful to re-generate the thumbnails after resizing the uploaded image. Therefore createThumbnail function should do the job.

This is from another forum post I found:

Code: Select all

createThumbnail($data['Picture'], array('width' => 750, 'height' => 750, 'identifier' => ''));
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 25.10 + all AppGini Helper tools

dge
Veteran Member
Posts: 31
Joined: 2013-12-05 02:54

Re: 23.13 mobile camera to image fields not working

Post by dge » 2024-04-25 21:50

Thanks Jan, that's very helpful.
So I had a programmer try to implement this in a hook but he can't seem to get it working. Do you see anything obviously wrong?

Here is the hook he wrote:

function products_after_update($data, $memberInfo, &$args){

$filename = $data['photo'];
$width = 200;
$height = 200;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);

createThumbnail($data['photo'], array('width' => 49, 'height' => 49, 'identifier' => ''));

return TRUE;

}


---------------------------

He ALSO tried doing the thumbnail separately, in the after_update hook (also doesn't seem to work)

function products_after_update($data, $memberInfo, &$args){

createThumbnail($data['photo'], array('width' => 49, 'height' => 49, 'identifier' => ''));

return TRUE;

}

dge
Veteran Member
Posts: 31
Joined: 2013-12-05 02:54

Re: 23.13 mobile camera to image fields not working

Post by dge » 2024-04-30 23:43

Ok, for anyone needing to resize images before saving, here's a hook that's working for us now -(The code isn't perfect, it assumes the images are in 4x3 ratio so, it can always be improved!)

What we were missing before is simply that you need to give the full path to the images on the server. I believe this path is defined somewhere in appgini if you want to make it a variable.

Code: Select all

 function products_before_update(&$data, $memberInfo, &$args) {

$filename = $data['photo'];

//Get dimensions of the image 
list($width, $height) = getimagesize('https://yourdomain.com/database/images/'."$filename");

$ratio = $width/$height;

if ($width > 200 || $height > 200)
{   
// Change width and height 
if ($ratio >= 1)
{
$new_width = 200 * $ratio;
$new_height = 200;
}
else
{
$new_width = 266;
$new_height = 266 * $ratio;
}
   
// Resample the image 
$image = imagecreatefromjpeg('https://yourdomain.com/database/images/'.$filename);
$image_p = imagecreatetruecolor($new_width, $new_height); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
   
//write new file to server
imagejpeg($image_p, 'images/'. $filename);

//save new file to database
$data['photo'] = $filename;
}
		return TRUE;
	}

Post Reply