Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PNG Resampling Program not working.

Featured Replies

This program is supposed to take large pngs and resize them so their widths and heights are no larger than 700px.

 

I wrote the same program for jpegs, and a similar one for pngs, but for some reason this particular program doesn't want to work. When I put in a large png (width and height are both over 700px), it'll output "The width is 3072 and the height is 2048," something like that, but it doesn't create or display the smaller image it's supposed to make.

 


<?php

$filename = '1.png';

$sizearray = getimagesize($filename);
$width = $sizearray[0];
$height = $sizearray[1];
echo "The width is $width and the height is $height. ";

if ($width > $height && $width > 700)
{
$new_height = ($height * 700)/$width;
$new_width = 700;
}
elseif ($width < $height && $height > 700)
{
$new_width = ($width * 700)/$height;
$new_height = 700;
}
elseif ($width == $height && $width > 700)
{
$new_height = 700;
$new_width = 700;
}
else{
echo "No resizing needed";
}

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagepng($image_p, "s4.png", 3);

echo "<img src='s4.png'>";
?>

I'm no PHP expert and this probably won't solve the problem but have you looked at SWITCH instead of all those if/else statements?

Here's a function you can use to rather pass the image and dimensions you want output!

 

function resizeImage($image, $width, $height){
   $tmp = split("\.", basename($image));
   $ext = $tmp[1];
   $src_img = "";

   foreach($tmp as $t) {
   	$ext = $t;
   }

   switch( $ext ){
       case "jpg": case "JPG": case "jpeg": case "JPEG": $src_img = imagecreatefromjpeg($image); break;
       case "png": case "PNG": $src_img = imagecreatefrompng($image); break;
   }

   $dst_img = imagecreatetruecolor($width, $height);

   $src_dimensions = getimagesize($image);
   $src_width = $src_dimensions[0];
   $src_height = $src_dimensions[1];

   //proportions
   $prop_src = $src_width/$src_height;
   $prop_dst = $width/$height;

   //automate
   if($prop_src > $prop_dst){
       $dst_height = $height;
       $dst_width = $src_width*$height/$src_height;
       //crops positions
       $prop_crop = $src_height/$height;
       $src_point_x = abs($src_width-$width*$prop_crop)/2;
       $src_point_y = 0;
   }else{
       $dst_width = $width;
       $dst_height = $src_height*$width/$src_width;
       //crops positions
       $prop_crop = $src_width/$width;
       $src_point_x = 0;
       $src_point_y = abs($src_height-$height*$prop_crop)/2;
   }

   //application of the parameters
   imagecopyresized($dst_img, $src_img, 0, 0, $src_point_x, $src_point_y, $dst_width, $dst_height, $src_width, $src_height);

   imagecopyresampled($dst_img, $src_img, 0, 0, $src_point_x, $src_point_y, $dst_width, $dst_height, $src_width, $src_height);

   switch( $ext ){
       case "jpg": case "JPG": case "jpeg": case "JPEG": imagejpeg($dst_img, $image); break;
       case "png": case "PNG": imagepng($dst_img, $image); break;
   }

}

  • Author

Thanks, this should help a lot. (:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.