phpzend-frameworklaminas

PHP crop and resize image on server and save to servermap


I upload a file with laminas/zend and do some validations for safety.

How can I crop and resize the image on the server and save the cropped/resized image to the same (server) directory as the original?

Code below is not working (the new file can not be found.)

$multiplier = 2;
$filename   = $form->getData()['profielfoto']['tmp_name'];

[$originalWidth, $originalHeight] = getimagesize($filename);

$newWidth  = $multiplier * $originalWidth;
$newHeight = $multiplier * $originalHeight;

$originalImage  = imagecreatefromjpeg($filename); 
$resultingImage = imagecreatetruecolor($newWidth, $newHeight);

imagecopyresized(
    $resultingImage, $originalImage,
    0, 0, 0, 0,
    $newWidth, $newHeight,
    $originalWidth, $originalHeight
);

Solution

  • try following for resizing the image:

    <?php
    
    $data = $form->getData();
    $uploadedFile = $data['profielfoto'];
    
    // get the original file info
    $originalPath = $uploadedFile['tmp_name'];
    $originalName = $uploadedFile['name'];
    $targetDirectory = dirname($originalPath); // original dir
    
    // create new filename
    $info = pathinfo($originalName);
    $resizedFilename = $info['filename'] . '_resized.' . $info['extension'];
    $resizedPath = $targetDirectory . '/' . $resizedFilename;
    
    // get dimensions from uploaded img
    list($originalWidth, $originalHeight) = getimagesize($originalPath);
    
    // calculate new dimensions
    $percent = 0.5; // that would be 50% percent of the original size
    $newWidth = $originalWidth * $percent;
    $newHeight = $originalHeight * $percent;
    
    $originalImage = imagecreatefromjpeg($originalPath);
    $resizedImage = imagecreatetruecolor($newWidth, $newHeight);
    
    // resize and save
    imagecopyresampled($resizedImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
    imagejpeg($resizedImage, $resizedPath, 90); // 90 is quality (0-100)
    
    // free up your memory
    imagedestroy($originalImage);
    imagedestroy($resizedImage);
    
    // now $resizedPath should contain the path to the resized image
    

    or if you want to crop the image heres some code for that too:

    <?php
    
    // define crop area
    $cropWidth = 200; // your crop width
    $cropHeight = 200; // desired crop height
    
    // calc position (here: center)
    $cropX = ($originalWidth - $cropWidth) / 2;
    $cropY = ($originalHeight - $cropHeight) / 2;
    
    // create the cropped image
    $croppedImage = imagecreatetruecolor($cropWidth, $cropHeight);
    imagecopyresampled($croppedImage, $originalImage, 0, 0, $cropX, $cropY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);
    
    // save cropped image
    $croppedFilename = $info['filename'] . '_cropped.' . $info['extension'];
    $croppedPath = $targetDirectory . '/' . $croppedFilename;
    imagejpeg($croppedImage, $croppedPath, 90);
    imagedestroy($croppedImage);
    

    (used from my project and modified)

    i hope that works for your code but please double check